2012-04-02 62 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(); 
} 
}