开发者

How to expand combo box on focus event?

开发者 https://www.devze.com 2022-12-14 02:03 出处:网络
I want to automatically expand Combo box on focus event. I have set the Droppeddown = True in gotfocus event, but this has a side effect. When click event gets fired, it expands dropdown and closes im

I want to automatically expand Combo box on focus event. I have set the Droppeddown = True in gotfocus event, but this has a side effect. When click event gets fired, it expands dropdown and closes immediately. How can I avoid it?

Here is Code:

Private 开发者_运维问答Sub cmbElectLoadPS_gotfocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbElectLoadPS.GotFocus
       cmbElectLoadPS.DroppedDown = True
End Sub


What about check if already DroppedDown ?

Private Sub cmbElectLoadPS_gotfocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbElectLoadPS.GotFocus
       if Not cmbElectLoadPS.DroppedDown Then
          cmbElectLoadPS.DroppedDown = True
       End If
End Sub

If u need this behavior for all your combo controls is better to create your own implementation

Pulic Class CustomComboBox
     Inherits ComboBox

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
           if Not DroppedDown Then
              DroppedDown = True
           End If
    End Sub

End Class


Oh .. add the same value to ComboBox on Mouseup event.. it would do the trick for u :) sthing like :

    private void comboBox1_Enter(object sender, EventArgs e)
    {

        comboBox1.DroppedDown = true;
    }

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

Not your best solution.. but it would do the trick :)


Create a timer called tmrDropDown (you should create a timer for each ComboBox) and leave its default properties. Add this code:

Private Sub cmbBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbBox.GotFocus
    tmrDropDown.Enabled = True
End Sub

And

Private Sub tmrDropDown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDropDown.Tick
    cmbBox.DroppedDown = True
    tmrDropDown.Enabled = False
End Sub


To implement this features for multiple combobox controls, rather than inheriting the combobox as a new custom control, I'd propose this simple solution:

Private Sub AutoDropDownCombobox_Enter(sender As Object, e As EventArgs) Handles _
    cboControl1.Enter, cboControl2.Enter ' register additional events here
    If Not CType(sender, ComboBox).DroppedDown Then
        CType(sender, ComboBox).DroppedDown = True
    End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消