views:

1201

answers:

6

Hello,

I've been having problems with the ComboBox control. I'm no expert in GUI, but I know this problem is related to the control's focus.

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

  1. I click on the ComboBox to list its items.
  2. I select one item. This closes the ComboBox.
  3. I click on the second ComboBox, the first one stays focused.

OR

  1. Click on a ComboBox (contains Point, Solid and Wireframe).
  2. Click on the form. Press either P, S or W. See the selection get changed.

Note that the ComboBox only has the DropDownStyle set to ComboBoxStyle.DropDownList. This means that it's the default ComboBox behavior. I thought that the default behavior was that the ComboBox would lose its focus when you clicked out of it, or on another control (button or ComboBox). It is not the case here, why?

UPDATE: What I need is some sort of ActiveComponent = null. The behavior should be similar to the one of Visual Studio were you select Debug or Release (ComboBox) in the standard toolbar. Currently, if I click outside of the ComboBox, it is still focused.

ComboBox visual description

+2  A: 

So what exactly are you saying? Are you saying that your _LostFocus() event handler is not being called? If so, the first place I would look is in your designer-generated event handler mapping code. Sometimes that has a way of being disassociated by doing certain things in the designer (it's rare these days, though...)

Dave Markle
I do not have defined any custom Leave event for the ComboBox, but if I do and set breakpoints, I can see it's clearly not called.
tomzx
+3  A: 

Are you sure the problem isn't because neither your frame or your other combobox have a way to gain focus?

novacara
+3  A: 

You may want to take a look at This topic. Try setting CausesValidation to false on the combo box, see if you can leave it. If an exception is thrown in the OnValidating event handler, it won't deselect the box.

Brandon
+2  A: 

I experienced a similar problem, but the control was recursively losing and regaining focus; the LostFocus event handler was being called, but the control immediately regained focus. Setting the CausesValidation property to False had no effect.

In my case, I had bound to the SelectedValue property instead of the Text property when binding to a custom object. Because I manually specified the ComboBox items collection and did not provide a DataSource, the ValueMember property was missing or invalid (so of course the SelectedValue property is no use.) Changing my binding to use the Text property solved the issue.

Rob
For my purposes I ended up choosing SelectedItem over Text, because apparently the TextChanged event only fires when the collection item is selected by the mouse, and not the keyboard. SelectedIndexChanged seems to fire regardless of input device.
Rob
+1  A: 

Hi, I had the similar problem and tried all the method you guys suggested. Unfortunately, none of them works. Here is my "simple" solution: send a "ESC" key stoke after you change the SelectedIndex.

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

It worked for me.

dufei
My solution is to set ActiveControl = null; instead of using SendKeys.Send("{ESC}");. It does the job and it's cleaner in term of code, but coding this behavior is not what I would expect.
tomzx
A: 

In ***form.Designer.vb you have some code for each combobox like:

'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 out the first line of code (includes string Forms.Binding("Text", ). So it seems only the statement for SelectedValue is required.

Anthony