0

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.

4

2 回答 2

2

这个问题很模糊,但考虑到从帖子中获得的信息,我会说遵循这种模式(伪代码):

using (StreamReader sr = new StreamReader(filepath)) 
 {

       while (sr.Peek() >= 0) 
       {
           string fileLine = sr.ReadLine();
           if(fileLine .Contains("hockey"))
                 DisplayInListBox(fileLine );
       }
}

像这样的东西。

于 2012-04-15T20:00:32.690 回答
0

我想这很微不足道:

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;
}

基本思路是缓存listbox的初始项,清空后根据文本框中输入的字符串进行填充。

于 2012-04-15T20:56:25.970 回答