【问题标题】:c# listbox live searchc# listbox live search
【发布时间】:2016-04-14 01:24:19
【问题描述】:

我正在学习 c#,我从制作一些虚拟应用程序开始,其中包含我可以练习的所有元素。我有搜索文本字段,下面有一个包含项目的列表框。

我尝试使用此代码,但只有从第一个字母开始搜索才能得到结果。我希望能够通过单词之间的字母进行搜索。

示例:列表项:“0445110085” 如果我从“0445”开始搜索,我会得到结果,但如果我从“5110”开始搜索,我会得到消息项未找到。

下面是我的代码,

private void searchBox_TextChanged(object sender, EventArgs e)
    {
        string myString = searchBox.Text;
        int index = listBox1.FindString(myString, -1);
        if (index != -1)
        {
            listBox1.SetSelected(index,true);
        }
        else 
            MessageBox.Show("Item not found!");
    }

提前致谢。

问候 :)

【问题讨论】:

标签: c# winforms visual-studio


【解决方案1】:

从FindString的细节来看,

查找 System.Windows.Forms.ListBox 中开头的第一项 指定的字符串。

因此,您将不得不自定义编写代码来实现它。像下面这样,

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string myString = textBox1.Text;
        bool found = false;
        for (int i = 0; i <= listBox1.Items.Count - 1; i++)
        {
            if(listBox1.Items[i].ToString().Contains(myString))
            {
                listBox1.SetSelected(i, true);
                found = true;
                break;
            }
        }                        
        if(!found)
        {
            MessageBox.Show("Item not found!");
        }                
    }

【讨论】:

  • @AleksandarMilic 不客气。快乐的编码和学习!
  • :D :D 我希望有一天我能像你们今天帮助我一样帮助别人。最好的问候
  • 这个解决方案有两个问题。第一个是次要的,可能会被作者接受(这种类型的经典搜索使用 StartsWith 方法而不是 Contains)。第二个是你没有检查列表的最后一项,所以它永远不会被发现。
  • @Seprum OP 想要在列表框中的项目中搜索他的搜索文本的存在。 StartsWith 将如何在这里达到目的? FindString 已经这样做了,但这不是问题所在。其次,验证所有项目直到列表框的末尾,直到找到匹配项。你怎么说最后一个项目永远找不到?
  • 看这部分:i &lt; listBox1.Items.Count - 1。无需减去1
【解决方案2】:

使用 StartsWith 方法检查特定项目是否以您输入的字符串开头:

private void searchBox_TextChanged(object sender, EventArgs e)
{
    string prefix = searchBox.Text;
    bool found = false;
    for(int i = 0; i < listBox.Items.Count; i++)
    {
        if(listBox.Items[i].ToString().StartsWith(prefix))
        {
            listBox.SelectedItem = listBox.Items[i];
            found = true;
            break;
        }
    }
    if(!found)
    {
        MessageBox.Show("Item not found!");
    }
}

【讨论】:

  • 我在第一次按键时出错。谢谢你帮助我。
  • 你能告诉我你收到了什么错误吗?
  • 已修复。立即尝试。
  • 错误 1“System.Windows.Forms.ListBox.ObjectCollection”不包含“Length”的定义,并且没有扩展方法“Length”接受“System.Windows.Forms”类型的第一个参数。可以找到 ListBox.ObjectCollection'(您是否缺少 using 指令或程序集引用?) C:\Users\shogy\Documents\Visual Studio 2010\Projects\WindowsFormsApplication4a\WindowsFormsApplication4\Form1.cs 55 48 WindowsFormsApplication4
  • 对不起。又修了。现在没有工作室:/
【解决方案3】:

肯定不是最好的解决方案,但它确实有效

    List<string> search = new List<string>();
    List<string> listBoxItems = new List<string>();
    listBoxItems = listBox1.Items.Cast<string>().ToList();

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text))
        {
            search.Clear();
            listBoxItems.ForEach(x =>
            {
                if (IsSubstring(textBox1.Text, x))
                {
                    search.Add(x);
                }
            });
            listBox1.DataSource = null;
            listBox1.DataSource = search;
        } 
        else
        {
            listBox1.DataSource = listBoxItems;
        }
    }

    public bool IsSubstring(string text, string x)
    {
        if (x.ToLower().Contains(text.ToLower()))
        {
            return true;
        } 
        else
        {
            return false;
        }
    }

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 2021-04-08
    • 1970-01-01
    • 2015-10-31
    • 2022-10-06
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多