有没有办法提高我的简单文本过滤器的性能?

发布于 2024-08-31 05:00:26 字数 893 浏览 1 评论 0原文

我正在编写一个过滤器来挑选项目。我有一个对象列表。这些对象包含数字、名称和一些其他不相关的项目。目前,该列表包含 200 项。在文本框中输入时,我正在查看字符串是否与列表中对象的数字/名称的一部分匹配。如果是这样,请将它们添加到列表框。下面是我的文本框 textchanged 事件的代码:

private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
    {
        lstOverview.Items.Clear();
        string data = "";
        foreach (ucTelListItem telList in _allUsers)
        {
            data = telList.User.H323 + telList.user.E164;
            if (data.Contains(txtTelnumber.Text))
                lstOverview.Items.Add(telList);
        }
    }

有时在输入字符时会出现一点延迟,特别是当我从 4 条记录变为 200 条记录时(因此,当我有一个过滤器且 4 条记录匹配时,我退格并显示整个列表)再次)。 我的列表是用户控件列表,因为我发现从列表加载用户控件所需的时间更少,然后每次都必须初始化一个新的用户控件。

我可以对代码做一些处理,还是只是将 usercontrol 添加到 listbox 导致小延迟(小延迟 = <1 秒)?

提前致谢。

编辑 我已经编辑了帖子,它是wpf。将项目放入列表并设置项目源并不能解决问题。

I'm writing a filter that will pick out items. I have a list of Objects. The objects contain a number, name and some other irrelevant items. At the moment, the list contains 200 items. When typing in a textbox, i'm looking if the string matches a part of the number/name of the objects in the list. If so, add them to the listbox. Here's the code for my textbox textchanged event :

private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
    {
        lstOverview.Items.Clear();
        string data = "";
        foreach (ucTelListItem telList in _allUsers)
        {
            data = telList.User.H323 + telList.user.E164;
            if (data.Contains(txtTelnumber.Text))
                lstOverview.Items.Add(telList);
        }
    }

I sometimes see a little delay when entering a character, especially when i go from 4 records to 200 records (so when i had a filter and 4 records matched, and i backspace and the whole list appears again).
My list is a list of usercontrols, cause I found it takes less time to load the usercontrols from a list, then to have to initialize a new usercontrol each time.

Can I do something about the code, or is it just adding the usercontrol the listbox that causes the small delay (small delay = <1 sec)?

Thanks in advance.

Edit
I've edited post, it's wpf. And putting items in a list and setting the itemssource isn't solving the problem.

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

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

发布评论

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

评论(6

爱你是孤单的心事 2024-09-07 05:00:26

我建议您同时使用两种技术:

  1. 在清除项目并将其添加到 ListBox 之前,调用 BeginUpdate() 方法,并调用其 EndUpdate( ) 当您完成添加项目时。这些方法专门用于避免大量插入项目期间的性能损失。
  2. 引入一个计时器,使过滤任务仅在 TextBox 的最后一个 KeyUp 事件经过特定时间后才开始。通过这种方式,您可以增加不评估对用户尚不重要的过滤器的机会。

I'd suggest you two techniques to be used in concert:

  1. Before clearing and adding item to the ListBox invoke the BeginUpdate() method, and invoke its EndUpdate() when you finish adding items. These methods are specifically there for avoiding performance loss during a massive insertion of items.
  2. Introduce a timer and make the filtering task start only after a specific amount of time has been elapsed after the last KeyUp event of the TextBox. In this way you augment the chance of not evaluating a filter that is not yet significant to the user.
小清晰的声音 2024-09-07 05:00:26

我刚刚找出导致将项目加载到列表框中的延迟的原因。我正在使用预定义的主题(Wpf 主题),并且因为我的列表框都是拉皮条的,所以重新绘制需要更长的时间。所以这与编程逻辑无关,只是样式延迟了我的过滤器。

I have just figured out what caused the delay on loading items into my listbox. I'm using predefined themes (Wpf Themes) and because my listbox is all pimped, it takes longer to be redrawn. So it's nothing to do with programming logic, just the style is delaying my filter.

无人接听 2024-09-07 05:00:26

使用 BeingUpdate/EndUpdate 包装代码,以在添加项目时停止重绘。

private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{
    lstOverview.BeginUpdate();
    lstOverview.Items.Clear();
    string data = "";
    foreach (ucTelListItem telList in _allUsers)
    {
        data = telList.User.H323 + telList.user.E164;
        if (data.Contains(txtTelnumber.Text))
            lstOverview.Items.Add(telList);
    }
    lstOverview.EndUpdate();
}

Wrap your code with BeingUpdate/EndUpdate to stop redrawing when adding the items.

private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{
    lstOverview.BeginUpdate();
    lstOverview.Items.Clear();
    string data = "";
    foreach (ucTelListItem telList in _allUsers)
    {
        data = telList.User.H323 + telList.user.E164;
        if (data.Contains(txtTelnumber.Text))
            lstOverview.Items.Add(telList);
    }
    lstOverview.EndUpdate();
}
玩世 2024-09-07 05:00:26
private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{ 
    lstOverview.DataSource=_allUsers.FindAll(delegate(ObjType telList)
    {
        return (telList.User.H323.Contains(txtTelnumber.Text) || telList.user.E164.Contains(txtTelnumber.Text) );
    });
}

尝试上面的代码

private void txtTelnumber_TextChanged(object sender, TextChangedEventArgs e)
{ 
    lstOverview.DataSource=_allUsers.FindAll(delegate(ObjType telList)
    {
        return (telList.User.H323.Contains(txtTelnumber.Text) || telList.user.E164.Contains(txtTelnumber.Text) );
    });
}

try the above code

若无相欠,怎会相见 2024-09-07 05:00:26

你只有200件???在 WPF 中,您不应该遇到任何性能滞后。只需将数据填充到 ObservableCollection 中,然后将其绑定到列表视图即可。现在,在您的 textchanged 事件中,您可以应用相同的过滤器逻辑,但应用到 ObservableCollection 而不是列表视图。列表视图应立即反映更改。

我正在处理数百万条记录,没有任何延迟。

您永远不想造成延迟

另请参阅VirtualMode 属性用于高级操作。

更新

看来您正在每个 textchanged 事件中执行此操作 data = telList.User.H323 + telList.user.E164; 。您可以更好地预先创建一个 List 并仅在循环内实现您的过滤器逻辑。

You have only 200 items??? You should not be experiencing any lag in performance then in WPF. Just populate your data into an ObservableCollection and inturn bind it to the listview. Now in your textchanged event you can apply the same filter logic but to the ObservableCollection instead of the listview. The listview should reflect the changes instantly.

I'm working with millions of records without any lag.

You never want to create delays

Also take a look at VirtualMode property for advanced operations.

Update

And it seems you're doing this operation data = telList.User.H323 + telList.user.E164; in every textchanged event. You can better create a List<data> beforehand and implement only your filter logic inside the loop.

妳是的陽光 2024-09-07 05:00:26

您必须首先使用 lambda 从列表中获取所有相关项目,然后尝试使用 AddRange 将项目添加到列表框。

You must first get all relevant items from your list by using lambda and then try to use AddRange for adding the items to the listbox.

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