【发布时间】: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 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();
}
}
}
}
【问题讨论】:
-
Accounts的类别是什么?代码只是说var... -
TextBox4代码写错了,请指正
-
foreach - 无法将 char 类型转换为字符串