将 CheckedListBox 中的选择更改为用户类型 [英] Change selection in CheckedListBox as user types

查看:21
本文介绍了将 CheckedListBox 中的选择更改为用户类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 VB.NET 应用程序中有一个多选复选框的自定义实现.它在很大程度上工作得很好,但我们最近用这个控件替换了一个普通的单选 ComboBox,它不支持边打字边搜索.

We have a custom implementation of a multi-select checkbox in our VB.NET application. It largely works just fine, but we recently replaced a regular single-select ComboBox with this control, and it does not support searching while typing.

例如,如果用户想要访问zygote"他们曾经能够开始输入这个词,而且它会慢慢接近.现在,当您键入时,它会跳转到 z、y、g 等等.
是否可以让它像使用标准 ComboBox 那样运行?

For example, if the user wants to get to "zygote" they used to be able to start typing the word and it would slowly get closer. Now, as you type, it jumps to the z's, then the y's, then the g's, and so on.
Is it possible to make it behave as it did with a standard ComboBox?

目前,我捕获 KeyDown 和 KeyUp,因此它不会在键入时额外选择项目,但这不是理想的最终解决方案.

For now, I capture KeyDown and KeyUp so it does not extraneously select an item as they type, but this is not the ideal final solution.

推荐答案

如评论中所述,Timer 可以工作,但我会忽略它(因为又老又无聊:),我会使用 秒表:

As described in the comments, a Timer can work, but I'll dismiss it (because old and boring :) and I'll make use of a Stopwatch instead:

每次生成 KeyDown 事件时,秒表都会重新启动.
按下的键被添加到 StringBuilder 对象(以避免创建大量字符串).当按键之间的时间间隔大于预定义值时,将清除 StringBuilder 容器:这里,我将其设置为 400ms,以测试或添加配置选项.

The Stopwatch is restarted each time a KeyDown event is generated.
The keys pressed are added to a StringBuilder object (to avoid the creation of a multitude of strings). The StringBuilder container is cleared when the time between key presses is greater than a predefined value: here, I've set it to 400ms, to test or add a configuration option.

► 双 StringBuilder.Append() 用于保留默认行为:当按键按下 长时间 延迟时,它会迭代以相同的字母(或多或少是文件资源管理器的作用).

► The double StringBuilder.Append() is there to preserve the default behavior: when keys are pressed with a long delay, it iterates the Items that begin with the same letter (more or less what File Explorer does).

KeyDown 处理程序添加到表单的 Sub New() 中,这里(添加到名为 checkedList1 的 CheckedListBox).它可用于处理 Form 中的任何 ListBox 或 CheckedListBox.

The KeyDown handler is added in the Form's Sub New(), here (to a CheckedListBox named checkedList1). It can be used to handle any ListBox or CheckedListBox in the Form.

Imports System.Diagnostics
Imports System.Text

Sub New() 
    AddHandler checkedList1.KeyDown, AddressOf listBox_KeyDown
End Sub

Private swLb As New Stopwatch()
Private sbCLb As New StringBuilder()

Private Sub listBox_KeyDown(sender As Object, e As KeyEventArgs)
    Dim checkedList = DirectCast(sender, ListBox)
    If e.KeyCode < Keys.A Then Return
    If swLb.ElapsedMilliseconds > 400 Then
        sbCLb.Clear()
        sbCLb.Append(ChrW(e.KeyData))
        swLb.Restart()
        Return
    End If
    e.SuppressKeyPress = True
    sbCLb.Append(ChrW(e.KeyData))
    Dim idx = checkedList.FindString(sbCLb.ToString())
    checkedList.SelectedIndex = If(idx = ListBox.NoMatches, checkedList.SelectedIndex, idx)
    swLb.Restart()
End Sub

这篇关于将 CheckedListBox 中的选择更改为用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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