I'm making a search function for a ListBox
, and I would like that as soon as the user types something into a TextBox
, all items are removed from the ListBox
except the item that matches the search text.
//files[i] are the files of the openfiledialog
List<String> ListboxItems = new List<String> {files[i]};
try
{
String search = gunaTextBox1.Text;
if (String.IsNullOrEmpty(search))
{
listBox1.Items.Clear();
listBox1.Items.AddRange(ListboxItems.ToArray());
}
var items = (from a in ListboxItems
where a.StartsWith(search)
select a).ToArray<String>();
listBox1.Items.Clear();
listBox1.Items.AddRange(items);
}
catch { }
Does anyone know how I can implement this?
Here is the solution
first you need to add a List String List<string> listcollection = new List<string>();
then you need to write the Textbox1_TextChanged event
List<string> listcollection = new List<string>();
private void gunaTextBox1_TextChanged(object sender, EventArgs e)
{
if(gunaTextBox1.Text == "Suche")
{
}
else
{
try
{
String search = gunaTextBox1.Text;
if (String.IsNullOrEmpty(search))
{
listBox1.Items.Clear();
listBox1.Items.AddRange(listcollection.ToArray());
}
var items = (from a in listcollection
where a.IndexOf(search, StringComparison.OrdinalIgnoreCase) > -1
select a).ToArray<String>();
listBox1.Items.Clear();
listBox1.Items.AddRange(items);
}
catch
{
}
}
}
and then just link it up
listcollection.Clear();
foreach(string str in listBox1.Items)
{
listcollection.Add(str);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.