C# 在 ListBox1 中搜索项目

我想通过 TextBox4 在 ListBox1 中做一个搜索项。此视频中的示例 - 倒带时间 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 上写代码,如果它不会让你复杂化。我把 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();
            }
        }
    }


}
灯塔

您无法将 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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C#-我的项目没有从listbox1移动到listbox2

将多个项目从 ListBox1 添加到绑定多个项目 ListBox2

如何检查 listbox2 是否包含一些 listbox1 项目 - VB.net

如何将项目添加到选定的Listbox1项目中的另一个Listbox2项目中?

将 ListBox1 中的每个 String 转换为 Integer 并添加到 ListBox2:格式不正确的输入字符串

在CheckedListBox C#中搜索项目

从 listBox c# 中获取随机项目

使用SQL查询从listbox1循环到listbox1到listbox2?可能吗?

如何在ListBox C#中检索每个项目

foreach 某些项目在 ListBox 中 - Winform,C#

将 ListBoxItems 从 ListBox1 拖放到 ListBox2 及其图像并避免重复 Delphi

如何使用listbox1添加的Listbox2 Texts文本项创建数组列表

比较listbox1项和listbox2项,并删除重复项

Excel VBA根据Listbox2选择填充Listbox1

排序C#ListBox项目

如何使用List中存在的功能。在C#中的列表中搜索项目时出错

将 DataGridView1 添加到 Listbox1 VB.net

C#如何继续搜索列表中的项目而不是仅在一次搜索后关闭的应用程序

为什么@UiFactory ListBox makeListBox1()方法会影响所有UiBinder ListBoxes(ListBox1,ListBox2,ListBox3等)?

c# asp.net - 在 DataList 中搜索项目并显示它们

C#如何将数组中的所有项目匹配到搜索词列表

如何使用ews manage api c#在Clutter文件夹中搜索项目

如何在C#中搜索组合框的所有项目?

如何在c#asp.net中搜索项目的结构列表?

在C#中将搜索字符串与arraylist中的项目匹配

项目没有出现在C#的ListBox中,但在XAML中是

在C中搜索结构

在C中搜索int *

在C#中搜索