VB.NET Listview Textchanged 搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13958512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:36:50  来源:igfitidea点击:

VB.NET Listview Textchanged Search

vb.netlistviewsearchfiltertextchanged

提问by Alex

I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.

我尝试在 stackoverflow 上搜索并实现我找到的代码,但无济于事。随着文本的变化,我想使用文本框中的当前文本来过滤列表视图项目(因此未关闭以匹配的项目将被删除),它只会让我保留列中包含的任何内容。

Here's an example of what I mean:

这是我的意思的一个例子:

Search: "Georges"

搜索:“乔治”

|1|Anderson Silva|Anderson silva is the champion in the ...|

|1|安德森席尔瓦|安德森席尔瓦是...|

|2|GeorgesSt-Pierre| Georgesis the champion in the ...|

|2| 乔治·圣皮埃尔| 乔治是...|的冠军

|3|GeorgesSotoropolis| GeorgesSotoropolis is a fighter in the lightweight division|

|3| 乔治·索托罗波利斯| GeorgesSotoropolis 是轻量级的战斗机|

With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.

通过此搜索,只会返回第 2 行和第 3 行。第一行将被省略并且不显示。一旦我删除了这些条款,它就会被显示出来。

Here is the code that I currently have:

这是我目前拥有的代码:

Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
    lwArticles.BeginUpdate()

    If tbSearch.Text.Trim().Length = 0 Then
        'Clear listview
        lwArticles.Clear()

        'If nothing is in the textbox make all items appear
        For Each item In originalListItems
            lwArticles.Items.Add(item)
        Next
    Else
        'Clear listview
        lwArticles.Clear()

        'Go through each item in the original list and only add the ones which contain the search text
        For Each item In originalListItems
            If item.Text.Contains(tbSearch.Text) Then
                lwArticles.Items.Add(item)
            End If
        Next
    End If
    lwArticles.EndUpdate()

End Sub

It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible

它似乎正在工作,但是一旦我在 tbSearch 中输入了一些东西,我就看不到列表视图项目。滚动条会变小/变大,这取决于执行搜索时是否有更多/更少的项目。我的问题似乎它们不可见

Thank you!

谢谢!

采纳答案by Sam Axe

Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clearinstead of lwArticles.Clear

Listview.Clear 清除项目、列和组。看来您只想清除物品,因此请致电lwArticles.Items.Clear而不是lwArticles.Clear

回答by Neolisk

I would suggest another approach - first create a DataTablebased on your original items. Then create a DataViewand assign that as DataSourceof your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChangedbecomes very simple and maintainable - it just changes RowFilter, if a corresponding DataViewhas already been created.

我建议另一种方法 - 首先DataTable根据您的原始项目创建一个。然后创建一个DataView并将其分配为DataSource您的ListView. 现在您可以更改DataView.RowFilter,您的列表将自动更新,因此只会显示与过滤器匹配的项目。使用这种方法,您不需要每次都从头开始重新创建所有内容,并且您TextChanged变得非常简单和可维护 -RowFilter如果DataView已经创建了相应的内容,它只会发生变化。

回答by Alex

Here's the final answer of my question:

这是我的问题的最终答案:

Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load

Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
    lwArticles.BeginUpdate()

    If tbSearch.Text.Trim().Length = 0 Then
        'Clear listview
        lwArticles.Items.Clear()

        'If nothing is in the textbox make all items appear
        For Each item In originalListItems
            lwArticles.Items.Add(item)
        Next
    Else
        'Clear listview
        lwArticles.Items.Clear()

        'Go through each item in the original list and only add the ones which contain the search text
        For Each item In originalListItems
            If item.Text.Contains(tbSearch.Text) Then
                lwArticles.Items.Add(item)
            End If
        Next
    End If

    lwArticles.EndUpdate()
End Sub

Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.

感谢 Dan-o 解释 lwArticles.Clear() 将清除所有内容。他通知我我需要 lwArticles.Items.Clear() 来只清除列表视图中的项目。