使用类中的组合框

时间:2013-10-20 16:19:58

标签: vb.net

我对使用组合框有一些疑问。

1)我需要从类中引用组合框:

If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
   something...
End If

从这里开始我需要从AND来检查这个组合框是否掉落但我不知道如何。

2)我的实际类型的组合框是“DropDownList”类型 问题是如果我将其放下并用向上/向下键输入,则根据所选行改变该值。如果我然后按ESC,那么最后一个值保持选择不想要的。

如果在列表被删除时按ESC键,这里是从丢弃时刻返回原始值的方法吗?
怎么做?

这里仔细看看我的xCombo子类,以获得第二个问题的帮助......

Public Class xAutoCombo
Inherits ComboBox

Private entertext As String

Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)

    If e.Control Then ' this dropped a list with keyboard
        If e.KeyCode = Keys.Down Then
            Me.DroppedDown = True
        End If
    End If

    '' this should return value back if ESC was pressed
    '' but don't work!
    If Me.DroppedDown And e.KeyCode = Keys.Escape Then 
        Me.Text = entertext
    End If

    MyBase.OnKeyDown(e)
End Sub

Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)

    entertext = Me.Text '' this remember text in moment of droping
    MyBase.OnDropDown(e)
End Sub

编辑:
在这里,我发现了一个我希望解决的功能问题 当组合被删除并且我通过键盘浏览列表然后用鼠标按下以形成(或在组合之外)它关闭列表并设置最后被选择的值。

而不是我希望该组合仅在点击鼠标列表或用键盘按Enter键时设置他的新值。

1 个答案:

答案 0 :(得分:1)

检查DroppedDown属性,但看起来你在删除时还想做其他事情。

Dim cbo As ComboBox
If Me.ActiveControl.GetType Is GetType(ComboBox) then
    cbo=Ctype(Me.ActiveControl, ComboBox)

    ' make it easier to refernece

    if cbo.DroppedDOwn then
        ....
    End iF
End if

' Note:
Ctype(Me.ActiveControl, ComboBox).DroppedDown
' should work, but the above is easier to read and use since you apparently 
' will have other things to do/override with it

另请注意,我认为三种组合框下拉类型中的一种不使用/支持DroppedDown属性。

对于你不完全遵循的其他问题,你也可以存储最后选择的项目并以类似的方式恢复它。但是,覆盖Windows默认行为很少是一个好主意,因为您正在创建用户以前从未遇到的内容。

修改

将Escape功能从CLOSE DROPDOWN更改为ABORT CHOICE: 注意:我刚使用std cbo,refs需要更改为MyBase,事件更改为On...

Private selectedindex As Integer = -1
Private bEsc As Boolean = False

Private Sub cbo_Enter(....
   ' reset tracking vars...might not be needed
    selectedindex = -1
    bEsc  = False
End Sub

Private Sub cbo_DropDown(...
    ' capture starting state
    selectedindex = cbo.SelectedIndex
    bEsc = False
End Sub

的KeyDown:

    If cbo.DroppedDown AndAlso e.KeyCode = Keys.Escape Then
        bEsc = True
        e.Handled = True
        ' this MUST be last!
        cbo.DroppedDown = False
    End If


 Private Sub cbo_DropDownClosed(...
    ' rest cbo.selectedindex if escape was pressed
    If bEsc Then
       ' note: SelectedIndexChanged event will fire and any code there will run
       ' may need to qualify with If Esc = False...
        cbo.SelectedIndex = selectedindex
    End If
End Sub