I have an ASP.NET ListBox that displays a list of activites taken from a text file. Now what I want to do is to search words, for example "hockey", entered by the user in a TextBox control, and display in the ListBox only the activities containing that search string.
Asked
Active
Viewed 1,143 times
2 Answers
2
The question is vague enough, but considering information got from the post, I would say follow this pattern (a pseudocode):
using (StreamReader sr = new StreamReader(filepath))
{
while (sr.Peek() >= 0)
{
string fileLine = sr.ReadLine();
if(fileLine .Contains("hockey"))
DisplayInListBox(fileLine );
}
}
Something like this.

Tigran
- 61,654
- 8
- 86
- 123
-
1Looks like he already read data from a file since already bound it to a ListBox. Also code which mixed togethe file read operations an UI update looks not quite good from my point of view, why just not separate low-leve file-read functionality from UI stuff? – sll Apr 15 '12 at 20:08
-
@sll: to be honest, not very clear.. cause in both cases he talks about `activites` – Tigran Apr 15 '12 at 20:12
0
pretty trivial I guess:
var items = //listBox1.Items;
private void textBox1_TextChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (object s in items)
{
if (s.ToString().Contains("hockey"))
listBox1.Items.Add(s);
}
if (listBox1.Items.Count > 0)
listBox1.SelectedIndex = 0;
}
The basic idea is to cache listbox's initial items, and clear it and then fill according to string typed in textbox.

nawfal
- 70,104
- 56
- 326
- 368