且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个文本框列表框过滤项目

更新时间:2023-10-14 12:34:16

THX给大家,但我做了简单的东西..
希望帮助..

thx to everyone but i made something simpler.. hope that helps ..

声明一个列表:

       List<string> list = new List<string>();

在主窗口:

      public MainWindow() {
        list.Clear();

        foreach (String str in lb1.Items)
        {
            list.Add(str);
        }
     }

在TextChanged事件:

In the textchanged event:

      public void t1_TextChanged(object sender, TextChangedEventArgs e)
{
        if (String.IsNullOrEmpty(t1.Text.Trim()) == false)
        {
            lb1.Items.Clear();
            foreach (string str in list)
            {
                if (str.StartsWith(t1.Text.Trim()))

                {
                    lb1.Items.Add(str);
                }
            } 
        }

        else if(t1.Text.Trim() == "")
        {
            lb1.Items.Clear();

            foreach (string str in list)
                {
                    lb1.Items.Add(str);
                }
            }                         
        }