输入textbox查找listbox 内容符合项

来源:互联网 发布:网络配线架 编辑:程序博客网 时间:2024/05/19 18:40
private List<string> items = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
    items.AddRange(new string[] { "Cat", "Dog", "Carrots", "Brocolli" });
    foreach (string str in items)
        listBox1.Items.Add(str);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    foreach (string str in items)
        //if (str.StartsWith(textBox1.Text, StringComparison.CurrentCultureIgnoreCase)) //从某个字母开始
        //    listBox1.Items.Add(str);
        if (str.Contains(textBox1.Text.ToString())) //包含某个字母
            listBox1.Items.Add(str);
}
原创粉丝点击