【问题标题】:VB.NET Listview Textchanged SearchVB.NET Listview Textchanged 搜索
【发布时间】:2012-12-07 04:00:19
【问题描述】:

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

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

搜索:“乔治”

|1|安德森席尔瓦|安德森席尔瓦是...的冠军|

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

|3|乔治 Sotoropolis| Georges Sotoropolis 是轻量级的斗士|

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

这是我目前拥有的代码:

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

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

谢谢!

【问题讨论】:

  • “不起作用”没有足够详细地描述问题。详细说明什么不起作用。
  • 什么也没发生。换句话说,我还不如不把代码放在那里,因为它除了删除所有东西之外什么都不做。
  • 那是什么?代码什么都不做,还是代码删除了所有内容?
  • 代码删除一切
  • 我编辑了我的代码。更干净 - 现在唯一的问题是我看不到修改。似乎发生了一些事情,但我看不到列表视图项目,它是空白的。然而,滚动条变小/变大取决于列表视图中有多少搜索结果。

标签: vb.net listview search filter textchanged


【解决方案1】:

这是我问题的最终答案:

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

感谢 Dan-o 解释 lwArticles.Clear() 将清除一切。他通知我,我需要 lwArticles.Items.Clear() 来仅清除列表视图中的项目。

【讨论】:

  • 如果其他人提供了答案,不鼓励发布您自己的答案。自我回答“游戏”了系统,不鼓励人们回答你的问题。 SO 依靠信誉系统来激励良好的答案。 FAQ可以详细解释。 TLDR 版本是,不需要发布最终代码,得到一个时标记正确的答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 非常感谢,这就是我所缺少的:D!
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多