我想通过 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] 删除。
我来说两句