过滤器列表框在文本框实时 [英] Filter ListBox with TextBox in realtime

查看:136
本文介绍了过滤器列表框在文本框实时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个文本框,实时过滤带文本的列表框。

I am trying to filter an listbox with text from a textbox, 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();
}

下面是问题:


  1. 当我运行程序我得到这个错误:未设置为一个对象的实例对象引用

如果我打了退格键,是不是显示了我的初步名单。这是因为我的实际项目的列表现在是减少了,但我怎么能做到这一点?

If I hit backspace, my initial list is not shown anymore. This is because my actual list of items is now reduced, but how can I achieve this?

你能指出我在正确的方向?

Can you point me in the right direction?

推荐答案

这是很难从代码只是扣除,但我的设定的你从不同方面出生的滤波问题:

It's hard to deduct just from the code, but I presume your filtering problem born from the different aspects:

a)您需要一个模式上显示的数据的的ListBox 。您需要您持有某个地方(词典数据库 XML项目的colleciton BinaryFile 收藏),某种的商店的简而言之。

a) You need a Model of the data shown on ListBox. You need a colleciton of "Items" which you hold somewhere (Dictionary, DataBase, XML, BinaryFile, Collection), some kind of Store in short.

要显示在用户界面中的数据您总是来自的商店的,过滤它挑中的数据,并把它放在UI。

To show the data on UI you always pick the data from that Store, filter it and put it on UI.

二)第一点后,你的过滤代码可以看起来像这样(的伪代码的)

b) After the first point your filtering code can look like this (a pseudocode)

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();



希望这有助于。

Hope this helps.

这篇关于过滤器列表框在文本框实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆