首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >利用ListBox对TextBox进行实时滤波

利用ListBox对TextBox进行实时滤波
EN

Stack Overflow用户
提问于 2012-04-02 20:57:16
回答 6查看 37.8K关注 0票数 7

我正在尝试使用文本框realTime中的文本筛选列表框。

以下是代码:

代码语言:javascript
运行
复制
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

  • If我点击了backspace,我的初始列表不再显示了。这是因为我的实际项目列表现在已经减少了,但是我如何实现这一点呢?

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

EN

Stack Overflow用户

回答已采纳

发布于 2012-04-02 21:08:51

很难仅从代码中扣除,但我认为您的过滤问题是从不同方面产生的:

( a)您需要一个显示在Model上的数据的ListBox。你需要一个收藏“物品”的地方(DictionaryDataBaseXMLBinaryFileCollection),简而言之,就是某种商店。

要在UI上显示数据,总是从存储区中选择数据,过滤并放到UI中。

( b)在第一点之后,过滤代码可以如下所示(伪代码)

代码语言:javascript
运行
复制
var registrationsList = DataStore.ToList(); //return original data from Store

registrationListBox.BeginUpdate();
registrationListBox.Items.Clear();

if(!string.IsNullOrEmpty(SrchBox.Text)) 
{
  foreach (string str in registrationsList)
  {                
     if (str.Contains(SrchBox.Text))
     {
         registrationListBox.Items.Add(str);
     }
  }
}
else 
   registrationListBox.Items.AddRange(registrationsList); //there is no any filter string, so add all data we have in Store

registrationListBox.EndUpdate();

希望这能有所帮助。

票数 11
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9983720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档