c# 在下一个按键事件处中断 TextBox 中正在执行的按键事件

发布于 2024-09-27 15:03:16 字数 963 浏览 0 评论 0原文

我正在使用文本框的输入来过滤列表框中显示的信息。我希望每次按下按键时都会进行过滤。

我面临的问题是,如果用户输入第二个、第三个等字符,过滤器将在每次按键时继续执行。例如,用户输入“a”,过滤开始,在完成字母“a”的过滤之前,用户添加字母“b”,因此完整的搜索字符串现在是“ab”。

此时,我想要发生的是第一个过滤器完全停止并从头开始在“ab”上启动新过滤器。

private bool Interrupt = false;
private bool Searching = true;

private void tbxFilter_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Searching)
    {
        Interrupt = true;
    }
}

private void tbxFilter_KeyUp(object sender, KeyEventArgs e)
{
    Searching = true;

    foreach (MyObject o in ListOfMyObjects)
    {
        if (Interrupt)
        {
            Interrupt = false;
            break;
        }
        else
        {
            DoFilter(o);
        }
    }

    Searching = false;
}

上面的代码大致显示了我目前所拥有的内容,删除了一些特定于应用程序的部分(例如类型和对象名称)。

理想情况下,我想做的是能够在第二次按键开始时暂停(直到搜索已设置为 false - 表明所有其他搜索已退出),但添加一个 while 循环来检查只会锁定此时的程序。

有没有办法实现这个目标?

谢谢

编辑:显示项目的控件是 ListBox / ListView 控件。

I am using the input from a TextBox to filter information displayed in a ListBox. I want the filtering to occur each time a key is pressed.

The problem I am faced with is that if the user types a second, third etc. character, the filter will carry on through for each of the key presses. E.g. The user types 'a' and the filtering starts, before filtering for the letter 'a' is finished, the user adds the letter 'b' so the complete search string is now 'ab'.

At this point, what I want to happen is for the first filter to stop completely and start the new filter on 'ab' from scratch.

private bool Interrupt = false;
private bool Searching = true;

private void tbxFilter_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Searching)
    {
        Interrupt = true;
    }
}

private void tbxFilter_KeyUp(object sender, KeyEventArgs e)
{
    Searching = true;

    foreach (MyObject o in ListOfMyObjects)
    {
        if (Interrupt)
        {
            Interrupt = false;
            break;
        }
        else
        {
            DoFilter(o);
        }
    }

    Searching = false;
}

The code above shows roughly what I have at the moment with some application specific pieces removed (like Types and object names).

What i would like ideally to do is be able to pause at the beginning of the second key press (until Searching has been set to false - indicating that all other searches have exited), but adding a while loop to check for that will just lock the program at that point.

Is there a way to a way to accomplish this?

Thanks

EDIT: The control that is displaying the items is a ListBox / ListView control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-04 15:03:16

我会创建一个工作线程来进行过滤。当启动新的过滤器进程时,正在运行的工作线程将被取消(通过设置标志或取消令牌,请参阅.net 4.0 任务类)。

当线程完成时,您可以通过一个操作获取结果并将其显示在 GUI 线程上。

I would create a worker thread for the filtering. When a new filter process is initiated the running worker thread is canceled (by a setting a flag or cancel token, see .net 4.0 Task class).

When the thread is finished, you take the result and display it in one action on the GUI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文