我想从文本框实时过滤带有文本的列表框。实时过滤带有文本框的列表框
下面是代码:
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();
}
这里有问题:
当我运行程序我得到这个错误:
Object reference not set to an instance of an object
如果我打退格,我初始列表不再显示。这是因为我的实际物品清单现在已经减少,但我怎么能做到这一点?
您能否指点我正确的方向?
你尝试使用(!的IsPostBack)检查,如果它没有后回 – 2012-04-02 21:07:21
你需要保持在一个单独的'名单列表框中的内容'让Items.Clear()不要”给你留下一个空白列表。 NRE并不那么明显。如果原始项目不是字符串,则转换为字符串不一定有效。始终使用ToString()。 –
2012-04-02 21:29:34
@COLDTOLD:很确定这是一个WinForms问题... – 2012-04-02 21:37:45