2012-04-02 59 views
3

我想从文本框实时过滤带有文本的列表框。实时过滤带有文本框的列表框

下面是代码:

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. 当我运行程序我得到这个错误:Object reference not set to an instance of an object

  2. 如果我打退格,我初始列表不再显示。这是因为我的实际物品清单现在已经减少,但我怎么能做到这一点?

您能否指点我正确的方向?

+0

你尝试使用(!的IsPostBack)检查,如果它没有后回 – 2012-04-02 21:07:21

+1

你需要保持在一个单独的'名单列表框中的内容'让Items.Clear()不要”给你留下一个空白列表。 NRE并不那么明显。如果原始项目不是字符串,则转换为字符串不一定有效。始终使用ToString()。 – 2012-04-02 21:29:34

+1

@COLDTOLD:很确定这是一个WinForms问题... – 2012-04-02 21:37:45

回答

5

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

a)您需要在ListBox显示的数据的Model。您需要您持有某个地方 “项目”(DictionaryDataBaseXMLBinaryFileCollection),某种商店短的colleciton。

为了表示对UI的数据,你总是商店挑中的数据,筛选,并把它放在UI。

B)第一点后,你的过滤代码可以是这样的(的伪

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

希望这有助于。

+0

是的,这是过滤的答案。 (修改了一下) – observ 2012-04-03 08:06:38

0

像这样的事情可能会为你工作:

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

ListBox,而不是ListView。 – 2012-04-02 21:26:55

+0

@HansPassant:谢谢。不知何故,我错过了,即使我几次输入'registrationListBox' ... :) – 2012-04-02 21:31:38

+0

嘿嘿,我知道这种感觉:) – 2012-04-02 21:33:21

0

是的,这就是答案的过滤。 (修改了一下)。我有一个文本文件中的信息。这是为我工作

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

看来这个错误:

Object reference not set to an instance of an object

是在我的代码别的地方,不能把我的手指上。

0

如果能够,将所有内容存储在字典中,并从那里填充它。

public partial class myForm : Form 
{ 
    private Dictionary<string, string> myDictionary = new Dictionary<string, string>(); 
//constructor. populates the items. Assumes there is a listbox (myListbox) and a textbox (myTextbox), named respectively 
public myForm() 
{ 
    InitializeComponent(); 
    myDictionary.Add("key1", "item1"); 
    myDictionary.Add("key2", "My Item"); 
    myDictionary.Add("key3", "A Thing"); 

    //populate the listbox with everything in the dictionary 
    foreach (string s in myDictionary.Values) 
     myListbox.Add(s); 
} 
//make sure to connect this to the textbox change event 
private void myTextBox_TextChanged(object sender, EventArgs e) 
{ 
    myListbox.BeginUpdate(); 
    myListbox.Items.Clear(); 
    foreach (string s in myDictionary.Values) 
    { 
     if (s.Contains(myListbox.Text)) 
      myListbox.Items.Add(s); 
    } 
    myListbox.EndUpdate(); 
} 
}