如何在焦点事件上展开组合框?

时间:2009-12-17 09:50:58

标签: vb.net winforms combobox

我想在焦点事件上自动展开组合框。 我在gotfocus事件中设置了Droppeddown = True,但这有副作用。当点击事件被触发时,它会扩展下拉列表并立即关闭。我怎么能避免它?

这是代码:

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

4 个答案:

答案 0 :(得分:4)

如果已经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

如果您需要针对所有组合控件执行此操作,则最好创建自己的实现

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

答案 1 :(得分:0)

哦..在Mouseup事件上为ComboBox添加相同的值.. 它会为你做的伎俩:) sthing喜欢:

    private void comboBox1_Enter(object sender, EventArgs e)
    {

        comboBox1.DroppedDown = true;
    }

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

不是你最好的解决方案......但是它可以解决问题:)

答案 2 :(得分:0)

创建一个名为tmrDropDown的计时器(您应该为每个ComboBox创建一个计时器)并保留其默认属性。 添加以下代码:

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

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

答案 3 :(得分:0)

要为多个组合框控件实现此功能,而不是将组合框作为新的自定义控件继承,我建议使用这个简单的解决方案:

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