AutoText for ListBox

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I've got a ListBox on my form with lots of data in it (14000 entries).

I have to put a search field on the form. As text is entered into the search field, I want the ListBox to remove entries that do not match. Sounds simple enough, but it is really taking a long time! I started running it before opening my web browser, and now (with typing this message, I'm about to go over and hit a breakpoint) it is at ... 12687 entries, and all I've entered is one character into my filtering TextBox.

Why is my version taking so long?

What can I do to speed it up?
C#:
private void Filter(string value)
{
  for (int i = ListView1.Items.Count - 1; -1 < i; i--)
  {
    if (ListView1.Items[i].SubItems[1].Text.StartsWith(value) == false)
    {
      ListView1.Items[i].Remove();
    }
  }
  ListView1.Refresh();
}
 
14,000 entries is an awful lot for a listbox, is there any way this could be reduced?

How is the listbox populated? If the data is in a data table you could bind to a data view instead and use it's filtering capabilities.

Would it be possible to wait until a character or two have been typed and then bind to items that match rather than removing non-matches?

If you are using VS 2005 or higher you could try using a text box and populate the auto complete list with your 14,000 strings.
 
Using VS 2005.

I tried using the Auto Complete setting (set AutoCompleteMode to SuggestAppend, AutoCompleteSource to CustomSource, and provided an AutoCompleteCustomSource after collecting the data), but Auto Complete didn't seem to do anything.

Looking up Auto Complete, MSDN told me that the maximum number of Auto Complete values depends on the OS (rather vague). I presumed I was over running the buffer and it crapped-out on me (technical term).

The data _does_ come from a table, but it has been messaged for readability where I have added additional columns. I can't seem to find out how to databind something that also has manually added columns. Lots of examples! ...but not many that step outside of the realm of automation.
 
Back
Top