Vb.net 使用类中的combobox
我对combobox的使用有一些问题 1) 我需要从类中引用combobox,如下所示:Vb.net 使用类中的combobox,vb.net,Vb.net,我对combobox的使用有一些问题 1) 我需要从类中引用combobox,如下所示: If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then something... End If 从这里,我需要从和的权利,以检查这个组合框是下降,但我不知道如何 2) 我的组合框的实际类型是“DropDownList”类型。 问题是,如果我将其下拉并使用向上/向下键键入,则该值将根据所选行
If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
something...
End If
从这里,我需要从和的权利,以检查这个组合框是下降,但我不知道如何
2) 我的组合框的实际类型是“DropDownList”类型。问题是,如果我将其下拉并使用向上/向下键键入,则该值将根据所选行进行更改。如果我按ESC键,则最后一个值保持为choosen不需要的值 如果在列表被删除时按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键时设置其新值。检查
下拉属性,但似乎在下拉时还有其他要做的事情
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默认行为很少是个好主意,因为您正在创建用户以前从未遇到过的东西
编辑
要将转义功能从关闭下拉列表更改为中止选项,请执行以下操作:
注意:我刚刚使用了标准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
向下键:
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
不,它不在DataGridView中,而在standalone中。但我尝试将其子类化,以获得少量必需的函数。通过活动进行协调是可以的。啊!好啊很抱歉我将删除上面的评论。请尝试:如果Me.DroppedDown并选择e.KeyCode=Keys.Escape,那么
…我也不完全确定您的一些Me
引用不应该是MyBase
…而不是捕获文本,我将捕获selectedindex或SelectedItem问题是代码运行良好,通过调试,我在entertext字符串中看到了正确的值。但是combobox只是不想在退出时获取此文本。。。现在我想,这里可能存在一些用于取消选择的内置功能?我让它工作了…它需要太多的代码a)我知道这不是一个好主意b)代码会导致其他问题。我想要没有旗帜的解决方案。在阅读瞬间直接从combobox读取状态。还有一个'DroppedDown'属性,但你的问题似乎暗示你还有其他东西要合并DroppedDown会很好。但是如何从课堂上得到它呢?我举了一个例子“如果Me.ActiveControl.GetType是GetType(ComboBox)和combodroppeddown=False”,那么,如何用DroppedDown替换combodroppeddown=False呢?请看编辑…我不知道你在说什么…你的代码中没有一个是优秀的!就这样,谢谢你。如果我不在课堂上,那么我就可以直接引用。但是班级太大了,不能全部张贴在这里。关于选择价值的第二个问题?