[英]How to convert listbox.items to a Array
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.我正在为
ListBox
制作一个搜索功能,我希望只要用户在TextBox
键入内容,除了与搜索文本匹配的项目之外,所有项目都将从ListBox
中删除。
//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>();
首先你需要添加一个List String
List<string> listcollection = new List<string>();
then you need to write the Textbox1_TextChanged event那么你需要编写Textbox1_TextChanged事件
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.