一个文本框列表框过滤项目 [英] filter listbox items with a textbox

查看:76
本文介绍了一个文本框列表框过滤项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListBox 一些项目文本框。在文本框文本应该与的ListBox 项目字符>&安培;应显示过滤的结果。如何做到这一点?
感谢。

I have a ListBox with some Items, and a TextBox. The text in TextBox should match starting characters of Items in ListBox & should display filtered result. How to do this? Thanks.

推荐答案

THX给大家,但我做了简单的东西..
希望帮助..

thx to everyone but i made something simpler.. hope that helps ..

声明一个列表:

       List<string> list = new List<string>();

在主窗口:

      public MainWindow() {
        list.Clear();

        foreach (String str in lb1.Items)
        {
            list.Add(str);
        }
     }

在TextChanged事件:

In the textchanged event:

      public void t1_TextChanged(object sender, TextChangedEventArgs e)
{
        if (String.IsNullOrEmpty(t1.Text.Trim()) == false)
        {
            lb1.Items.Clear();
            foreach (string str in list)
            {
                if (str.StartsWith(t1.Text.Trim()))

                {
                    lb1.Items.Add(str);
                }
            } 
        }

        else if(t1.Text.Trim() == "")
        {
            lb1.Items.Clear();

            foreach (string str in list)
                {
                    lb1.Items.Add(str);
                }
            }                         
        }                

这篇关于一个文本框列表框过滤项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆