Winforms: how to open combobox properly?

后端 未结 5 940
醉梦人生
醉梦人生 2020-12-06 16:26

I have a combobox on the winforms. On Enter even I open it:

cbo.DroppedDown = true;

But if I do that the combo opens and closes immediately

相关标签:
5条回答
  • 2020-12-06 16:38

    I tried it just like this:

    private void comboBox1_enter(object sender, EventArgs e)
          {
             comboBox1.DroppedDown = true;
          }
    

    no changes to mouseup or timers. it behaved just as expected. Whether I selected the comboBox with a mouse click or tabbed into it the drop down list appeared and stayed open until I selected something.

    I would look to see if there is something else pulling focus off the box.

    0 讨论(0)
  • 2020-12-06 16:43

    The reason you are having this problem is because the mouseup event occurs after the enter event and the default window procedure is closing the combobox.

    In the enter you could check the mouse button status and if the button is down, do not open the combo. Then have another event handler for the mouseup event to open the combo.

    Another option is to set a timer for a few milliseconds, and open the combo when it goes off.

    0 讨论(0)
  • 2020-12-06 16:49

    Knowing this is a bit old, but I found that this works well. You can TAB into the combo box at it opens and if you click the arrow it doesn't close back up.

    private void ComboBox_Enter(object sender, EventArgs e)
        {            
            if (MouseButtons == MouseButtons.None)
                ((System.Windows.Forms.ComboBox)sender).DroppedDown = true;
        }
    
    0 讨论(0)
  • 2020-12-06 16:53

    I think you just need to focus it first before open the comboBox.

    cbo.Focus();
    cbo.DroppedDown = True
    

    Hope it works for you.

    0 讨论(0)
  • 2020-12-06 16:58

    Set DroppedDown = true in GotFocus event of the combobox. Otherwise, the dropdown list will show at wrong location.

    void cbo_GotFocus(object sender, EventArgs e)
        {
            ComboBox cbo = sender as ComboBox;
            cbo.DroppedDown = true;
        }
    
    0 讨论(0)
提交回复
热议问题