tomzx
tomzx

Reputation: 2033

ComboBox not losing focus

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.

Upvotes: 7

Views: 29570

Answers (13)

Mubsher Mughal
Mubsher Mughal

Reputation: 442

The way I did it as follow.

Step 1 - Create a method that returns all given controls on the form

    public IEnumerable<Control> GetAllControls(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();
        return controls.SelectMany(ctrl => GetAllControls(ctrl, type))
                                  .Concat(controls)
                                  .Where(c => c.GetType() == type);
    }

Step 2 - Create a custom event

    private void ChangeComboFocus(object sender, EventArgs e)
    {
        SendKeys.Send("{TAB}");
    }

Step 3 - Bind the event finally on form load

    private void ClientRegistrationForm_Load(object sender, EventArgs e)
    {
        var comboxes=_Helper.GetAllControls(this, typeof(ComboBox)).ToList();
        if (comboxes != null)
        {
            foreach (ComboBox item in comboxes)
            {
                item.SelectedIndexChanged +=new EventHandler(this.ChangeComboFocus) ;
            }
        }
    }

Upvotes: 0

Taja_100
Taja_100

Reputation: 449

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

or use

this code on control leave function :

 private void drp_Leave(object sender, KeyPressEventArgs e)
            {

                    SendKeys.Send("{TAB}");

            }

Upvotes: 0

Alexander Borochkin
Alexander Borochkin

Reputation: 4641

  1. After SendKeys.Send("{ESC}"); ComboBox still regains focus;

  2. Setting CausesValidation to false on the combo box didn't help me as well.

Here is how I resolved this problem:

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

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

in MouseClick EventHandler, which resolved the problem.

Upvotes: 1

Michael Korin
Michael Korin

Reputation: 56

I know this has been a while for this post, but maybe it would help someone in the future who comes across the same problem. I struggled for few days with this, but finally figures it out.

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

When you mind SelectedItem to the property like so

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

combobox calls the Equals method of the object that you use in the list which assigned to your DataSource. In my case, I needed to overwrite Equals method in this object. For whatever stupid reason, combobox calls this method and passes System.DBNull before actually passing the right object type for comparison. This is where violation occurred in my case and causing violation to fail, hence not releasing the cursor from the combobox. Also the weird part was that the program did not stop when Exception was caused in my Equals method.

Once I added this code

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

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

Upvotes: 0

user8588
user8588

Reputation: 1

The dictionary that combobox take the values has, type index, type value, the type index have to be the same type in your class properity bindingded on combobox. If the types was diferent the combobox never will lose focus.

Upvotes: 0

Anthony
Anthony

Reputation: 19

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.

Upvotes: 0

Rajesh
Rajesh

Reputation: 47

All you have to do is:

  1. go to property window of Combobox
  2. and set Allow Drop="true"

The property is intended for some other purpose but it works for this scenario too.

Upvotes: 3

Rob
Rob

Reputation: 5286

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 item collection and did not provide a data source, the ValueMember property was missing or invalid (so of course the SelectedValue property was no use.)

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

Upvotes: 2

ADI
ADI

Reputation: 21

Try Leave event instead of LostFocus.
Try Enter event instead of GotFocus.

Upvotes: 1

dufei
dufei

Reputation:

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.

Upvotes: 2

Brandon
Brandon

Reputation: 1279

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.

Upvotes: 19

novacara
novacara

Reputation: 2257

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

Upvotes: 3

Dave Markle
Dave Markle

Reputation: 97811

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...)

Upvotes: 2

Related Questions