a PROPER Searchbox? Searching a listbox..... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

a PROPER Searchbox? Searching a listbox.....

rdbandit1459rdbandit1459 Posts: 12Member
I would like to make a searchbox that searches a listbox.
When the text is changed in the searchbox the listbox is updated with the resulting mathches!

I am using the Following code, but this only works for a partial match. When the user types to many words or gets and exact match.... the program freezes!

Can anyone help? Here is the code I am using:

----------------------------------------------------------------------
private void txtSearch_TextChanged(object sender, System.EventArgs e)
{
// search the array list for matching records as the user types

// clear the results list
lstResults.Items.Clear();

// make sure there is something to search for
if (txtSearch.Text != "")
{
foreach(Person p in myAddressBook)
{
// try a partial string match, looking at the first few letters of each name only
if (p.Name != "")
{
if (p.Name.Substring(0, txtSearch.Text.Length) == txtSearch.Text)
{
// stick matches in the listbox
lstResults.Items.Add(p.Name);
}
}
}
}

}

-----------------------------------------------------------------
there must be a better way!!

Sign In or Register to comment.