小编obs*_*erv的帖子

使用TextBox实时过滤ListBox

我试图用文本框中的文本过滤一个列表框,realTime.

这是代码:

private void SrchBox_TextChanged_1(object sender, EventArgs e)
{
  var registrationsList = registrationListBox.Items.Cast<String>().ToList();
  registrationListBox.BeginUpdate();
  registrationListBox.Items.Clear();
  foreach (string str in registrationsList)
  {
    if (str.Contains(SrchBox.Text))
    {
      registrationListBox.Items.Add(str);
    }
  }
  registrationListBox.EndUpdate();
}
Run Code Online (Sandbox Code Playgroud)

以下是问题:

  1. 当我运行该程序时,我收到此错误: Object reference not set to an instance of an object

  2. 如果我点击退格键,我的初始列表将不再显示.这是因为我的实际物品清单现在减少了,但我怎样才能做到这一点?

你能为我指出正确的方向吗?

c# textbox listbox list winforms

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

c# ×1

list ×1

listbox ×1

textbox ×1

winforms ×1