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 user开发者_Go百科controls 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.
I'd suggest you two techniques to be used in concert:
- Before clearing and adding item to the
ListBox
invoke theBeginUpdate()
method, and invoke itsEndUpdate()
when you finish adding items. These methods are specifically there for avoiding performance loss during a massive insertion of items. - 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 theTextBox
. In this way you augment the chance of not evaluating a filter that is not yet significant to the user.
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.
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();
}
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
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.
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.
精彩评论