我有一个带有AutoComplete.SuggestAppend的数据绑定ListItems组合框 并且想要使用向上/向下箭头键从组合框导航到不同的控件,而不是滚动项目。
问题是如果文字没有完成,建议的文字会在下一个控件有焦点时保持高亮显示。
以下是一些代码,显示了我正在做的一个简单示例
Public Class Form1
Dim PreventCboBoxChanging As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New List(Of String)(New String() {10, 11, 20, 30})
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If PreventCboBoxChanging = True Then
e.Handled = True
End If
PreventCboBoxChanging = False
End Sub
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
PreventCboBoxChanging = True
TextBox1.Select()
End If
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.Select()
End If
End Sub
Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
Dim index As Integer = sender.FindString(sender.Text)
If index > -1 Then
sender.SelectedIndex = index
Else
e.Cancel = True
Me.Text = ""
Beep()
End If
End Sub
结束班
有没有办法取消选择文字?
答案 0 :(得分:1)
我在另一个帖子中找到了解决方案。需要关闭Combobox自动完成模式,更改焦点,然后在PreviewKeyDown事件下重新启用SuggestAppend模式。
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.AutoCompleteMode = AutoCompleteMode.None
TextBox1.Select()
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End If
End Sub
答案 1 :(得分:0)
这应该成功:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.SelectionLength = 0
End Sub