使用TextBox实时过滤ListBox
我试图用文本框中的文本过滤一个列表框,realTime。
这是代码:
private void SrchBox_TextChanged_1(object sender, EventArgs e) { var registrationsList = registrationListBox.Items.Cast().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
-
如果我点击退格键,我的初始列表就不再显示了。 这是因为我的实际物品清单现在减少了,但我怎样才能做到这一点?
你能为我指出正确的方向吗?
很难从代码中扣除,但我认为你的过滤问题源自不同的方面:
a)您需要ListBox
显示的数据Model
。 你需要一个“Items”的集合,你可以在某个地方( Dictionary
, DataBase
, XML
, BinaryFile
, Collection
),简而言之。
要在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();
希望这可以帮助。
这样的事可能适合你:
var itemList = registrationListBox.Items.Cast().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()); }
是的,这是过滤的答案。 (修改了一下)。 我在文本文件中有信息。 这对我有用
FileInfo registrationsText = new FileInfo(@"name_temp.txt"); StreamReader registrationsSR = registrationsText.OpenText(); var registrationsList = registrationListBox.Items.Cast().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();
似乎错误:
你调用的对象是空的
是从我的代码中的其他地方,不能把我的手指放在它上面。
如果能够,将所有内容存储在字典中,然后从那里填充它。
public partial class myForm : Form { private Dictionary myDictionary = new Dictionary(); //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(); } }
我会这样做:
private List registrationsList; private void SrchBox_TextChanged_1(object sender, EventArgs e) { registrationListBox.BeginUpdate(); registrationListBox.Items.Clear(); var filteredList = registrationList.Where(rl => rl.Contains(SrchBox.Text)) registrationListBox.Items.AddRange(); registrationListBox.EndUpdate(); }
只需记住在第一次填充列表框时填充registrationsList。
希望这可以帮助。