首页 > 解决方案 > 我的表单中的基本搜索,但有所不同

问题描述

我有一个组框,其中包含一个显示一堆 .txt 文件的列表框。在这个组框中还有一个文本框,我想用它来搜索文件列表。我已经在我的文本框 textchange 事件中添加了一些代码,但它所做的只是清除我的列表框,并且在退格键上,列表框没有重新填充它显示的 .txt 文件?蚂蚁帮助将不胜感激,thanx

private void custsearchbox_TextChanged(object sender, EventArgs e)
    {
        var itemList = custList.Items.Cast<string>().ToList();
        if (itemList.Count > 0)
        {
            //clear the items from the list
            custList.Items.Clear();

            //filter the items and add them to the list
            custList.Items.AddRange(
                itemList.Where(i => i.Contains(custsearchbox.Text)).ToArray());
        }
    }

标签: searchtextboxlistboxtext-filesgroupbox

解决方案


这有效:

listsup.Items.Clear();
        Supfile = System.AppDomain.CurrentDomain.BaseDirectory + "data\\Suppliers.txt";
        List<string> proName = new List<string>();
        using (StreamReader rdr = new StreamReader(Supfile))
        {
            string line;
            while ((line = rdr.ReadLine()) != null)
            {
                if (line.ToString().ToLower().Contains(supsearchtxt.Text))
                {
                    string[] val = line.Split(',');
                    listsup.Items.Add(val[0]);
                }
            }
        }

推荐阅读