ComboBox does not lose focus

I have problems with the ComboBox control. I am not an expert in the GUI, but I know that this problem is related to the focus of management.

For some reason, the ComboBox does not lose focus when I click on it. Say for example:

  • I click on ComboBox to list its elements.
  • I select one item. This closes the ComboBox.
  • I click on the second ComboBox, the first remains focused.

OR

  • Click on ComboBox (contains Point, Solid, and Wireframe).
  • Click on the form. Press either P, S, or W. See Change selection.

Please note that ComboBox has only DropDownStyle set to ComboBoxStyle.DropDownList. This means that this is the default behavior of the ComboBox. I thought the default behavior was that the ComboBox would lose focus when you exit it, or to another control (button or ComboBox). This is not so, why?

UPDATE: I need some kind of ActiveComponent = null. The behavior should be similar to the behavior of Visual Studio if you selected Debug or Release (ComboBox) on the standard toolbar. Currently, if I go beyond the ComboBox, it is still focused.

+4
source share
12 answers

You can take a look at this section . Try setting CausesValidation to false in the combo box, see if you can leave it. If an exception is thrown in the OnValidating event handler, it does not deselect.

+15
source

Are you sure that the problem is not that neither your frame nor your other combo boxes have the ability to get focus?

+3
source

All you have to do is:

  • go to the Combobox properties window
  • and set Allow Drop = "true"

The property is for other purposes, but it also works for this scenario.

+3
source

So what exactly are you saying? Are you saying that the _LostFocus () event handler is not called? If so, the first thing I would look at is the code of your event handler handler created by the developer. Sometimes he has a way to be disassociated by doing certain things in the designer (it's rare these days, though ...)

+2
source

I had a similar problem, but the control recursively lost and regained focus; the LostFocus event LostFocus was called, but control immediately returned focus. Setting the CausesValidation property to false did not affect.

In my case, I was bound to the SelectedValue property instead of the Text property when binding to a custom object. Since I manually specified a collection of ComboBox elements and did not provide a data source, the ValueMember property ValueMember missing or invalid (therefore, of course, the SelectedValue property was not used.)

Changing my binding to use the Text property resolved the issue.

+2
source

I had a similar problem and I tried the whole method suggested by you. Unfortunately, none of them work. Here is my β€œsimple” solution: send the β€œESC” key after changing SelectedIndex.

 ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e) { # do whatever you were doing ... ... SendKeys.Send("{ESC}"); } 

It worked for me.

+1
source

Try leaving the event instead of LostFocus.
Try entering an event instead of GotFocus.

+1
source
  • After SendKeys.Send("{ESC}") ; ComboBox still restores focus;

  • Setting CausesValidation to false in the combo box did not help me.

Here is how I solved this problem:

Suppose you have another control, for example, System.Windows.Forms.PictureBox pbxChart , and the user wants to move the focus right there by changing the values ​​in the combobox (selecting by Left Click or MouseWheel) . So I added:

  private void pbxChart_Click(object sender, EventArgs e) { pbxChart.Focus(); } 

in the MouseClick EventHandler that resolved the issue.

+1
source

In ***form.Designer.vb you have code for each view with a list:

 'OrgDetailsIDComboBox ' Me.OrgDetailsIDComboBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MedicoLegalBindingSource, "OrgDetailsID", True)) Me.OrgDetailsIDComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.MedicoLegalBindingSource, "OrgDetailsID", True)) Me.OrgDetailsIDComboBox.DataSource = Me.OrgBindingSource Me.OrgDetailsIDComboBox.DisplayMember = "Place" 

I fixed the problem by commenting on the first line of code (including the line Forms.Binding("Text", ) . Therefore, it seems that only the instruction for SelectedValue needed.

0
source

A dictionary that assigns combobox values ​​has a type index, a type value, the type index must be of the same type in the class property that is associated with combobox. If the types are different from each other, then the combobox will never lose focus.

0
source

I know that it was some time for this publication, but perhaps it would help someone in the future to face the same problem. I struggled with this for several days, but finally figured it out.

if you set CauseViolation to false, then you are not solving the problem, and the data binding stops working.

When you pay attention to SelectedItem on a property like this

 combobox.DataBindings.Add("SelectedItem", someObject, "MySelectedItemProperty", false, DataSourceUpdateMode.OnPropertyChanged) 

combobox calls the Equals method of the object you are using in the list that is assigned to your DataSource. In my case, I had to rewrite the Equals method in this object. For some stupid reason, combobox calls this method and passes System.DBNull before passing the correct object type for comparison. In this case, a violation occurred in my case and caused a violation, so without dropping the cursor from the drop-down list. Also the weird part was that the program did not stop when an exception was thrown in my Equals method.

As soon as I added this code

  if (obj.GetType() != this.GetType()) return false; 

to my Equals method, everything worked fine. Hope this helps someone.

0
source
  private void drp_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { SendKeys.Send("{TAB}"); } } 

or use

this code in vacation release function:

  private void drp_Leave(object sender, KeyPressEventArgs e) { SendKeys.Send("{TAB}"); } 
0
source

All Articles