我正在使用.NET Framework 4.0在VB上使用VB
我有一个组合框.它有一些项目,显示得很好.这里有点奇怪:
如果我单击组合框上的下拉箭头并单击我想要的项目,则调用SelectedIndexChanged – good.
如果我在组合框的文本区域内单击并开始键入我想要的内容并按向上(或向下)键完成它,则调用SelectedIndexChanged – 也很好.
如果我单击组合框上的下拉箭头并开始键入我想要的选项并按ENTER键完成它,则不会调用SelectedIndexChanged – 问题.
在最后一种情况下,是否存在由ENTER引起的不同事件?我尝试过使用TextChanged和TextUpdate事件,但这些事件似乎不起作用:
Private Sub cmbStatus_TextChanged(sender As System.Object,e As System.EventArgs) Handles cmbStatus.TextChanged If e.Equals(Keys.Enter) Then Call SomeMethod() End If
我应该使用除e.Equals(Keys.Enter)之外的东西吗?
还有其他我应该寻找的活动吗?
编辑:
ComboBox中的项目示例如下:
> 10 – 新进入和完成检查—>这是最常见的类型
> 13 – 分配给TRB / HRB —>有几个’/’
> 60 – 外部(保留直到另行通知)—>有一些'(‘和’)’
基本上,每个列表的类型是“## – SOME TEXT”.
Option Strict On Public Class Form1 Friend WithEvents ComboBox1 As New ComboBox With {.Parent = Me} Private Sub Form1_Load(ByVal sender As Object,ByVal e As EventArgs) Handles MyBase.Load ComboBox1.Items.AddRange({"hello","tes1ted","word","item","tes2ted"}) ComboBox1.Text = ComboBox1.Items(0).ToString End Sub Private Sub ComboBox1_KeyUp(ByVal sender As Object,ByVal e As KeyEventArgs) Handles ComboBox1.KeyUp 'You can put this in the keydown event,or adapt it a small bit and put it in the keypress event 'putting it in the textchanged event is problematic and not recommended. Dim OriginalText As String = ComboBox1.Text If e.KeyCode = Keys.Enter Then If ComboBox1.SelectionLength > 0 Then ComboBox1.Text = ComboBox1.Text ComboBox1.SelectionLength = 0 ComboBox1.SelectionStart = ComboBox1.Text.Length End If End If If Not IsTextKey(e.KeyCode) Then Exit Sub Dim Filter As String = ComboBox1.Text & "*" If Filter.Length = 1 Then Exit Sub For I = 0 To ComboBox1.Items.Count - 1 If LCase(ComboBox1.Items(I).ToString) Like LCase(Filter) Then ComboBox1.SelectedItem = ComboBox1.Items(I) ComboBox1.Select(OriginalText.Length,(ComboBox1.Text.Length - OriginalText.Length)) Exit Sub End If Next End Sub Function IsTextKey(ByVal Key As Integer) As Boolean Select Case True Case Key = Keys.Up : Return False Case Key = Keys.Down : Return False Case Key = Keys.Left : Return False Case Key = Keys.Right : Return False Case Key = Keys.Back : Return False Case Key = Keys.Delete : Return False Case Key = Keys.LWin : Return False Case Key = Keys.RWin : Return False 'add whatever I missed 'return false if the key either removes text from the textBox 'or does not produce a character Case Else 'return true if the key produces a visible character(including space) Return True End Select End Function End Class