使用建议附加在离开时取消选择组合框文本 [英] Deselect combobox text on leaving using suggestappend

查看:29
本文介绍了使用建议附加在离开时取消选择组合框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 AutoComplete.SuggestAppend 的数据绑定 ListItems 组合框并且想要使用向上/向下箭头键导航出组合框到不同的控件,而不是滚动项目.

I have a databound ListItems combobox with AutoComplete.SuggestAppend and would like to navigate out of the combobox to different controls using the up/down arrow keys rather than scrolling the items.

问题在于,如果文本未完成,则建议的文本会在下一个控件获得焦点时保持突出显示.

The issue is that if the text isn't completed the suggested text remains highlighted while the next control has focus.

图片示例链接

这是一些代码,显示了我正在做的事情的简单示例

Here is some code showing a simple example of what I am doing

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

结束课程

有没有办法取消选中文本?

Is there any way to deselect the text?

推荐答案

我在另一个帖子中找到了解决方案.需要关闭组合框自动完成模式,更改焦点,然后在 PreviewKeyDown 事件下重新启用 SuggestAppend 模式.

I found the solution in another thread. Needed to turn off the Combobox AutoComplete Mode, change focus then reenable SuggestAppend mode under the PreviewKeyDown event.

    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

这篇关于使用建议附加在离开时取消选择组合框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆