I would like to be able to filter a list containing 1000 lines with a length of 50-4000 characters, as the user types into the text field without delay.
I am currently using a timer that updates the list after a TextChangedtext field event has not been fired in 300 ms. However, it is quite jerky, and ui sometimes freezes for a moment.
What is the normal way to implement such functions?
Edit: I am using winforms and .net2.
thank
Here is a stripped down version of the code I'm currently using:
string separatedSearchString = this.filterTextBox.Text;
List<string> searchStrings = new List<string>(separatedSearchString.Split(new char[] { ';' },
StringSplitOptions.RemoveEmptyEntries));
if (this.unfilteredItems.Count == 0)
{
foreach (IMessage line in this.logMessagesListBox.Items)
{
this.unfilteredItems.Add(line);
}
}
StringComparison comp = this.IsCaseInsensitive
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
List<IMessage> resultingFilteredItems = new List<IMessage>();
foreach (IMessage line in this.unfilteredItems)
{
string message = line.ToString();
if(searchStrings.TrueForAll(delegate(string item) { return message.IndexOf(item, comp) >= 0; }))
{
resultingFilteredItems.Add(line);
}
}
this.logMessagesListBox.BeginUpdate();
this.logMessagesListBox.Items.Clear();
this.logMessagesListBox.Items.AddRange(resultingFilteredItems.ToArray());
this.logMessagesListBox.EndUpdate();
source
share