2012-04-02 26 views
3

テキストボックスからテキストを含むリストボックスをリアルタイムでフィルタリングしようとしています。ここでリストボックスをリアルタイムでTextBoxで表示する

はコードです:ここでは

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プロパティ)、それは後のバック –

+1

あなたは別の '一覧'ようItems.Clear()ドン」にリストボックスの内容を保存する必要がありますいないかどうかをチェックします空のリストを残す。 NREはあまり明らかではありません。元の項目が文字列でない場合、文字列へのキャストは必ずしも機能しません。常にToString()を使用します。 –

+1

@COLDTOLD:これはWinFormsの質問です –

答えて

5

それは単にコードから控除するのは難しいが、私はさまざまな側面から生まれた、あなたのフィルタリング問題を推定

A)あなたはListBoxに表示されるデータのModelを必要とします。 (DictionaryDataBaseXMLBinaryFileCollection)、ある種類のストアの短い「アイテム」の集まりが必要です。 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

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。 –

+0

@HansPassant:ありがとう。なんとなく、私は 'registrationListBox'を何度も打っていたとしてもそれを逃しました。:) –

+0

Hehe、私は感情を知っています:) –

0

はい。 (少し修正された)。私はテキストファイルの情報を持っていた。

Object reference not set to an instance of an object

はどこか私のコードでからで、それを私の指を置くことはできません。エラーがいるようです

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

可能であれば、すべてを辞書に格納してそこから入力してください。

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(); 
} 
} 
関連する問題