使用文本框搜索列表视图项目 [英] Search listview items using textbox

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

问题描述

我工作在C#中的简单应用(电话簿),并在项目中,我得到了充满联系人列表视图。我一直在试图实现自动(瞬间)通过使用列表视图的文本框搜索的可能性。我已设法使其工作,而不是在期望的方式。如果我给你举个例子,你会发现实际的问题。比方说,我有一个名为比尔·盖茨联系并当我试图寻找它 - 它被发现的那部分就可以了。但是,问题是,当我试图寻找另一个接触。在这种情况下,我需要清除文本之前,我键入另一个名称,但它有可能通过信函只删除信件。当我开始删除整个名称,删除它就像我刚才输入的名字第一个字母后 - 它选择的项目(和重点也一样) - 实际上没有时间删除整个名字再次找到一个接触前。我要删除第一个字母,然后切换回文本框,删除另一封信等是否有用于搜索是自动的任何解决方案 - 因为它是现在,但另一方面去除没有选择触点(清除文本框)是可能的。

看看在code:

 私人无效txt_Search_TextChanged(对象发件人,发送System.EventArgs)
    {
        如果(txt_Search.Text!=)
        {
            的foreach(在listView1.Items ListViewItem的项目)
            {
                如果(item.Text.ToLower()。包含(txt_Search.Text.ToLower()))
                {
                    item.Selected = TRUE;
                }
                其他
                {
                    listView1.Items.Remove(项目);
                }            }
            如果(listView1.SelectedItems.Count == 1)
            {
                listView1.Focus();
            }
        }
        其他
        {
            LoadContacts();
            RefreshAll();
        }
    }


解决方案

有一些事情错在你的code,首先通过它在一个循环修改集合时,我们不应该使用的foreach 虽然在某些情况下,它似乎工作,但不是真的,它必将成为未来陌生和迷惑你。我们应该以相反的顺序使用循环,而不是和循环。第二个错误就是将真正这可能会导致您的textBox失去焦点到ListView。解决的办法是,我们必须使用背景色,而不是用一些其他的方式来表示该项目被选中,如:

 私人无效txt_Search_TextChanged(对象发件人,发送System.EventArgs)
{
    如果(txt_Search.Text!=){
        的for(int i = listView1.Items.Count - 1; I> = 0;我 - ){
            VAR项目= listView1.Items [I]
            如果(item.Text.ToLower()。包含(txt_Search.Text.ToLower())){
                item.BackColor = SystemColors.Highlight;
                item.ForeColor = SystemColors.HighlightText;
            }
            其他{
                listView1.Items.Remove(项目);
            }
        }
        如果(listView1.SelectedItems.Count == 1){
            listView1.Focus();
        }
    }
    其他
        LoadContacts();
        RefreshAll();
    }
}

此外后用户聚焦的ListView ,所有的背景色前景色应复位,我们可以处理输入 的ListView事件

  //输入ListView1的事件处理程序
私人无效listView1_Enter(对象发件人,EventArgs的发送){
  的foreach(在listView1.Items ListViewItem的项目){
    item.BackColor = SystemColors.Window;
    item.ForeColor = SystemColors.WindowText;
  }
}

I am working on a simple application (phonebook) in C# and in my project I have got a listview filled with contacts. I have been trying to implement the possibility to automatically (instantly) search through a listview using a textbox. I have managed to make it work, but not in the desired way. You will realise the actual problem if I give you an example. Let's say that I have got a contact named Bill Gates and when I try searching for it - it gets found and that part is OK. But, the problem is when I try to search for another contact. In that case, I have to clear the textbox before I type another name, but it is possible to remove only letter by letter. When I start removing the whole name, after removing a first letter it acts like I have just entered the name - it selects the item (and focuses as well) - actually there is no time to remove the whole name before it finds a contact again. I have to remove a first letter, then switch back to the textbox, remove another letter etc. Is there any solution for searching to be automatic - as it is now, but on the other hand for removing (clearing the textbox) without selecting contacts to be possible.

Take a look at the code:

private void txt_Search_TextChanged(object sender, System.EventArgs e)
    {
        if (txt_Search.Text != "")
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Text.ToLower().Contains(txt_Search.Text.ToLower()))
                {
                    item.Selected = true;
                }
                else
                {
                    listView1.Items.Remove(item);
                }

            }
            if (listView1.SelectedItems.Count == 1)
            {
                listView1.Focus();
            }
        }
        else 
        { 
            LoadContacts();
            RefreshAll();
        }
    } 

解决方案

There are some things wrong in your code, firstly when modifying a collection in a loop through it, we should not use foreach although in some case it seems to work but not really, it will surely be strange in future and confuse you. We should use a for loop instead and loop in the reverse order. The second wrong thing is you set the Selected to true which may cause your textBox lose focus to the listView. The solution is we have to use some other way to indicate that the item is selected, such as by using BackColor instead:

private void txt_Search_TextChanged(object sender, System.EventArgs e)
{
    if (txt_Search.Text != "") {
        for(int i = listView1.Items.Count - 1; i >= 0; i--) {
            var item = listView1.Items[i];
            if (item.Text.ToLower().Contains(txt_Search.Text.ToLower())) {
                item.BackColor = SystemColors.Highlight;
                item.ForeColor = SystemColors.HighlightText;
            }
            else {
                listView1.Items.Remove(item);
            }
        }
        if (listView1.SelectedItems.Count == 1) {
            listView1.Focus();
        }
    }
    else   
        LoadContacts();
        RefreshAll();
    }
}

Also after user focusing the ListView, all the BackColor and ForeColor should be reset, we can handle the Enter event of ListView:

//Enter event handler for listView1
private void listView1_Enter(object sender, EventArgs e){
  foreach(ListViewItem item in listView1.Items){
    item.BackColor = SystemColors.Window;
    item.ForeColor = SystemColors.WindowText;
  }
}

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

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