【问题标题】:C# Search items in a ListBox1C# 在 ListBox1 中搜索项目
【发布时间】:2018-05-16 21:58:05
【问题描述】:

我想通过 TextBox4 在 ListBox1 中进行搜索。 本视频中的示例 - 倒带时间 7.20 https://www.youtube.com/watch?v=7J-D4OzfX7Y; 但我没有数据库。我有一个类来存储数据。 而且我不知道如何执行搜索以找到我需要的项目。文件 - http://dropmefiles.com/bVrnX , Foreach - Error, could not convert char type to stringClassForm1SeeForm2。如果它不会使您复杂化,您能否在 TextBox4 上编写代码。我把TextBox4代码写错了,请指正

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();
            }
        }
    }


}

【问题讨论】:

  • Accounts 的类别是什么?代码只是说var...
  • TextBox4代码写错了,请指正
  • foreach - 无法将 char 类型转换为字符串

标签: c# search listbox


【解决方案1】:

您将无法将 chartype 转换为字符串异常,因为您在 Account.Name 字符串本身上调用 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2016-04-01
    相关资源
    最近更新 更多