首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

回答 6

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

Stack Overflow用户

发布于 2012-04-02 21:12:17

像这样的东西可能对你有用:

代码语言:javascript
运行
复制
var itemList = registrationListBox.Items.Cast<string>().ToList();
if (itemList.Count > 0)
{
    //clear the items from the list
    registrationListBox.Items.Clear();

    //filter the items and add them to the list
    registrationListBox.Items.AddRange(
        itemList.Where(i => i.Contains(SrchBox.Text)).ToArray());
}
票数 0
EN

Stack Overflow用户

发布于 2012-04-03 08:09:35

是的,这就是过滤的答案。(修改了一点)。我在文本文件里找到了信息。这就是对我起作用的东西

代码语言:javascript
运行
复制
FileInfo registrationsText = new FileInfo(@"name_temp.txt");
            StreamReader registrationsSR = registrationsText.OpenText();
            var registrationsList = registrationListBox.Items.Cast<string>().ToList();

            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
                while (!registrationsSR.EndOfStream)
                {
                    registrationListBox.Items.Add(registrationsSR.ReadLine());
                }
            registrationListBox.EndUpdate();

似乎这一错误:

对象引用未设置为对象的实例

来自我代码中的其他地方,不能把手指放在上面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9983720

复制
相关文章

相似问题

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