Is there a way to improve the performance of my simple text filter?

I am writing a filter that will select items. I have a list of objects. Objects contain a number, a name, and some other irrelevant elements. The list currently contains 200 items. As I type textbox

, I'm looking to see if the string matches part of the number / name of the objects in the list. If so, add them to listbox

. Here's the code for my altered text 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 slight delay in typing 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 came up again). My list is a list of usercontrols because I found it takes less time to load custom items from the list and then initialize a new user control each time.

Is there something I can do with the code, or is it just adding usercontrol

the listbox

which causes a little latency (small latency = <1 sec)?

Thanks in advance.

Edit I edited the post, this is wpf. And placing the items in the list and setting the itemssource doesn't solve the problem.

+2


a source to share


6 answers


I offer you two methods to be used at the concert:



  • Before clearing and adding an item to, ListBox

    call the method BeginUpdate()

    and call it EndUpdate()

    when you're done adding items. These methods are specifically designed to prevent performance degradation when inserting elements massively.
  • Enter a timer and start the filtering task only after a certain time has elapsed since the last KeyUp

    event TextBox

    . Thus, you increase the likelihood of not evaluating a filter that is not yet significant to the user.
+2


a source


I just figured out what was causing the delay in loading items into my list. I am using predefined themes (Wpf Themes) and because my list is boxed it takes longer to redraw. So it has nothing to do with programming logic, just style is delaying my filter.



+1


a source


Wrap your code with a Genesis / EndUpdate function to stop redrawing when elements are added.

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();
}

      

0


a source


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

0


a source


You only have 200 items ??? You shouldn't experience any performance lag then in WPF. Just fill the data into an ObservableCollection and inturn bind it to the list. Now, in your text event, you can apply the same filter logic, but to the ObservableCollection instead of the listview. The list should reflect the changes immediately.

I work with millions of records without any delay.

You never want to create delays

Also consider the VirtualMode property for advanced operations.

Update

And it seems that you are performing this operation data = telList.User.H323 + telList.user.E164;

on every event changed in the file. You can better create List<data>

beforehand and implement just your filter logic inside the loop.

0


a source


You should first get all matching elements from your list using a lambda and then try to use AddRange to add the elements to the list.

0


a source







All Articles