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.
a source to share
I offer you two methods to be used at the concert:
- Before clearing and adding an item to,
ListBox
call the methodBeginUpdate()
and call itEndUpdate()
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
eventTextBox
. Thus, you increase the likelihood of not evaluating a filter that is not yet significant to the user.
a source to share
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();
}
a source to share
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.
a source to share