Filter ListBox with TextBox in realtime
It's hard to deduct just from the code, but I presume your filtering problem born from the different aspects:
a) You need a Model
of the data shown on ListBox
. You need a colleciton of "Items" which you hold somewhere (Dictionary
, DataBase
, XML
, BinaryFile
, Collection
), some kind of Store in short.
To show the data on UI you always pick the data from that Store, filter it and put it on UI.
b) After the first point your filtering code can look like this (a pseudocode)
var registrationsList = DataStore.ToList(); //return original data from Store
registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();
if(!string.IsNullOrEmpty(SrchBox.Text))
{
foreach (string str in registrationsList)
{
if (str.Contains(SrchBox.Text))
{
registrationListBox.Items.Add(str);
}
}
}
else
registrationListBox.Items.AddRange(registrationsList); //there is no any filter string, so add all data we have in Store
registrationListBox.EndUpdate();
Hope this helps.