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
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
theLizardPosted Oct 30, 2009, 2:51 AM
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...
naura paxPosted Oct 30, 2009, 2:33 AM
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.