代码之家  ›  专栏  ›  技术社区  ›  Den

C#在列表框中搜索项目1

  •  0
  • Den  · 技术社区  · 6 年前

    我想通过文本框4在列表框1中搜索项目。 本视频中的示例-倒带时间7.20 https://www.youtube.com/watch?v=7J-D4OzfX7Y ; 但我没有数据库。我有一个用来存储数据的类。 我不知道如何进行搜索以找到我需要的物品。文件- http://dropmefiles.com/bVrnX , Foreach - Error, could not convert char type to string , Class , Form1See , Form2 .如果不让你复杂化的话,你能在TextBox4上写代码吗。我把文本框4的代码写错了,所以请改正

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            if (Properties.Settings.Default.Accounts == null)
                Properties.Settings.Default.Accounts = new List<Account>();
    
            if (Properties.Settings.Default.Accounts != null)
            {
           foreach (var account in Properties.Settings.Default.Accounts)
                {
                    listBox1.Items.Add(account);
                }
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            new Form2(listBox1).Show();
        }
    
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
    
    
    
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.Accounts != null)
            {
                foreach (var account in Properties.Settings.Default.Accounts)
                {
    
    
                    var registrationsList = account.Name;
    
    
                    listBox1.BeginUpdate();
                    listBox1.Items.Clear();
    
                    if (!string.IsNullOrEmpty(textBox4.Text))
                    {
                        foreach (string str in registrationsList)
                        {
                            if (str.Contains(textBox4.Text))
                            {
                                listBox1.Items.Add(str);
                            }
                        }
                    }
                    else
                        listBox1.Items.Add(account); //there is no any filter string, so add all data we have in Store
    
                    listBox1.EndUpdate();
                }
            }
        }
    
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Farlos    6 年前

    您无法将ChartType转换为字符串异常,因为您正在该帐户上调用foreach。名称字符串本身,它遍历帐户名中的每个字符。我重写了这个方法,以便正确地更新过滤器字符串的listBox1。

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        if (Properties.Settings.Default.Accounts != null)
        {
            listBox1.BeginUpdate();
            listBox1.Items.Clear();
            if (!String.IsNullOrEmpty(textBox4.Text))
            {
                foreach (var account in Properties.Settings.Default.Accounts)
                {
                    if (account.Name.Contains(textBox4.Text))
                    {
                        listBox1.Items.Add(account);
                    }
                }
    
            }
            else //there is no any filter string, so add all data we have in Store
            {
                foreach (var account in Properties.Settings.Default.Accounts)
                {
                    listBox1.Items.Add(account);
                }
            }
    
            listBox1.EndUpdate();
        }
    }