简体   繁体   English

C# ListView 搜索项目没有明确的列表

[英]C# ListView search item without clear list

I have winform project on C# platform.我在 C# 平台上有 winform 项目。 I have listview and textbox as you see in pic below.我有列表视图和文本框,如下图所示。 I want to reorder the list according to the text value entered by the user.我想根据用户输入的文本值对列表进行重新排序。

I researched before ask here, I generally saw solutions based on removing and re-adding all units to listview again.我在这里问之前进行了研究,我通常看到基于将所有单元再次删除并重新添加到列表视图的解决方案。 I don't want to do that because my listview has too many items with pictures so removing and re-adding items causes listview to work slowly.我不想这样做,因为我的列表视图有太多带有图片的项目,因此删除和重新添加项目会导致列表视图工作缓慢。

What I want is that, when the user enters characters in the textbox, the items which starts with this characters, bring this items the top of the list something similar google search system.我想要的是,当用户在文本框中输入字符时,以这个字符开头的项目,把这个项目放在列表的顶部,类似于谷歌搜索系统。

I tried the codes below but this send the item at the end of the list even though i chose index 0. Thanx.我尝试了下面的代码,但是即使我选择了索引 0,这也会将项目发送到列表的末尾。谢谢。

在此处输入图像描述

private void txt_search_TextChanged(object sender, EventArgs e)
        {
            string text = txt_search.Text;
            var item = listView1.FindItemWithText(text);
            if (item != null)
            {
                int index = listView1.Items.IndexOf(item);

                if (index > 0)
                {
                    listView1.Items.RemoveAt(index);
                    listView1.Items.Insert(0, item);
                }
            }
        }

ListView is sorted using the .Sort() function, not sure what the default behaviour is, but I think you need a custom comparer. ListView使用.Sort() function 进行排序,不确定默认行为是什么,但我认为您需要一个自定义比较器。

Here is an example implementation by (ab)using the ListViewItem.Tag .这是(ab)使用ListViewItem.Tag的示例实现。

Custom Comparer:自定义比较器:

private class SearchCompare : Comparer<ListViewItem>
{
    public override int Compare([AllowNull] ListViewItem x, [AllowNull] ListViewItem y)
    {
        if (x?.Tag != null && y?.Tag != null)
        {
            return x.Tag.ToString().CompareTo(y.Tag.ToString());
        }
        return 0;
    }
}

Initializing the ListView :初始化ListView

var items = new[]
{
    "1 no",
    "2 yes",
    "3 no",
    "4 yes"
};
foreach (var item in items)
{
    listView1.Items.Add(item);
}
listView1.ListViewItemSorter = new SearchCompare(); // custom sorting

And ofcourse the text changed event handler:当然,文本更改事件处理程序:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string text = textBox1.Text;
    foreach (ListViewItem item in listView1.Items)
    {
        if (item.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) > -1)
        {
            item.Tag = "a"; // a is sorted before b
        }
        else
        {
            item.Tag = "b"; // b is sorted after a
        }
    }
    listView1.Sort();
}

Typing "yes" in the search textbox will sort items 2 and 4 in front of items 1 and 3.在搜索文本框中键入“yes”会将项目 2 和 4 排在项目 1 和 3 的前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM