Searching a list box using a text box
I would like to know in Visual C# how to search/filter a list box using a text box.
Thanks,
tibois
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
naura paxRank #2,843 · 502 reputationPosted Oct 29, 2009, 10:33 PM EDT
If it is a winform app then
1. Use DataTable as a datasource for the listbox,set valuemember (eg Id) and displaymeber property (eg Name) as well.
2. Store that datatable in your program as global variable.
2. Add a keypress event handler for textbox and in keypress event and in that handler filter global datatable using Select method
eg. DataRow[] drArr=dtGlobal.Select("Name='" + txt.Text + "'");
DataTable dtNew = new DataTable();
for (int i = 0; i < drArr.Length; i++)
dt.Rows.Add(drArr[i].ItemArray);
now set dtNew as datasource of ListBox.
listBox1.DataSource=dtNew.
try it and tell me.
theLizardRank #1,079 · 1.8K reputationPosted Oct 29, 2009, 10:51 PM EDT
for(int i=0; i
if(ListBox1.Items[i].Text..Substring(0, TextBox1.Length) == TextBox1.Text)
{
ListBox.Items[i].SelectedIndex =i;
break;
}
}
what this will do is track the characters entered into the text box and select the list box item that matches the characters entered.
Hope this helps you...