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

C#在列表框中搜索项目1,c#,search,listbox,C#,Search,Listbox,我想通过文本框4在列表框1中搜索项目。 本视频中的示例-倒带时间7.20; 但我没有数据库。我有一个用来存储数据的类。 我不知道如何进行搜索以找到我需要的物品。档案- , Foreach-错误,无法将字符类型转换为字符串。,,。如果不会使您复杂化,您可以在TextBox4上编写代码吗。我把TextBox4的代码写错了,请改正 public partial class Form1 : Form { public Form1() { InitializeCompone

我想通过文本框4在列表框1中搜索项目。 本视频中的示例-倒带时间7.20; 但我没有数据库。我有一个用来存储数据的类。 我不知道如何进行搜索以找到我需要的物品。档案- ,
Foreach-错误,无法将字符类型转换为字符串。
,,。如果不会使您复杂化,您可以在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();
            }
        }
    }


}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
if(Properties.Settings.Default.Accounts==null)
Properties.Settings.Default.Accounts=新列表();
if(Properties.Settings.Default.Accounts!=null)
{
foreach(Properties.Settings.Default.Accounts中的var帐户)
{
列表框1.项目.添加(帐户);
}
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
新表单2(listBox1.Show();
}
私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
私有void textBox4\u TextChanged(对象发送方,事件参数e)
{
if(Properties.Settings.Default.Accounts!=null)
{
foreach(Properties.Settings.Default.Accounts中的var帐户)
{
var registrationsList=账户名称;
listBox1.BeginUpdate();
listBox1.Items.Clear();
如果(!string.IsNullOrEmpty(textBox4.Text))
{
foreach(注册列表中的字符串str)
{
if(str.Contains(textBox4.Text))
{
列表框1.Items.Add(str);
}
}
}
其他的
listBox1.Items.Add(account);//没有任何筛选器字符串,因此请添加存储区中的所有数据
listBox1.EndUpdate();
}
}
}
}

您无法将ChartType转换为字符串异常,因为您正在对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();
    }
}

那些
账户的类别是什么?代码只是说
var
…我把TextBox4代码写错了,所以请更正它,因为它无法将字符类型转换为字符串