[英]How to convert listbox.items to a Array
我正在為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 { }
有誰知道我如何實現這一點?
這是解決方案
首先你需要添加一個List String List<string> listcollection = new List<string>();
那么你需要編寫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
{
}
}
}
然后把它鏈接起來
listcollection.Clear();
foreach(string str in listBox1.Items)
{
listcollection.Add(str);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.