vb.net 如何在 ListView 中搜索列的特定项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19294389/
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
How to search a specific item of a column in ListView?
提问by EmPlusPlus
In my Listview there are 4 columns and there is a TextBox. How to let user to search a string in second column of the Listview?
在我的 Listview 中有 4 列,并且有一个 TextBox。如何让用户在 Listview 的第二列中搜索字符串?
回答by The Hungry Dictator
Private Function getListItemByName(query As String) As ListViewItem
For Each lvi As ListViewItem In lst.Items
If lvi.Text.Equals(query) Then Return lvi
For Each si As ListViewItem.ListViewSubItem In lvi.SubItems
If si.Text.Equals(query) Then Return lvi
Next
Next
Return Nothing
End Function
http://www.dreamincode.net/forums/topic/237883-search-for-an-item-in-listview/
http://www.dreamincode.net/forums/topic/237883-search-for-an-item-in-listview/
回答by tinstaafl
One way is to use LINQ:
一种方法是使用 LINQ:
Public Function SearchLV(lv As ListView, SearchString As String, Colmn As Integer) As List(Of ListViewItem)
SearchLV = (From item In lv.Items.OfType(Of ListViewItem)()
Where item.SubItems(Colmn).Text = SearchString
Select item).ToList
If SearchLV.Count = 0 Then
SearchLV.Add(New ListViewItem("Your search returned no results"))
End If
End Function
This function will return a list of listview items that match the search term in a specific column. The colmn argument is the zero based index of the column. If no items are found the first item in the list will contain that message.
此函数将返回与特定列中的搜索词匹配的列表视图项列表。colmn 参数是列的从零开始的索引。如果未找到任何项目,列表中的第一项将包含该消息。
to retrieve the items simply iterate through the list:
要检索项目,只需遍历列表:
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
Dim searchstring As String = TextBox1.Text
Dim searchlist As List(Of ListViewItem) = Search(ListView1, searchstring, 2)
Dim mssage As String = ""
For Each item In searchlist
For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
mssage += subitem.Text + "-"
Next
mssage = mssage.Substring(0, mssage.Length - 1) + vbNewLine
Next
MessageBox.Show(mssage)
End Sub
If you want a partial word search change:
如果您想要部分单词搜索更改:
Where item.SubItems(Colmn).Text = SearchString
to:
到:
Where item.SubItems(Colmn).Text.Contains(SearchString)
回答by CvikDasa
This is what works for me it's easy to understand and easy to modify. With this you can search specifified rows in listview.
这对我有用,它易于理解且易于修改。有了这个,您可以在列表视图中搜索指定的行。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each info As ListViewItem In ListView1.Items
Dim NameCheck As String = 0
Dim SurnameCheck As String = 0
Dim IDCheck As String = 0
NameCheck = info.Text
SurnameCheck = info.SubItems(1).Text
IDCheck = info.SubItems(2).Text
If NameCheck = TextBox1.Text Then
If SurnameCheck = TextBox2.Text Then
If IDCheck = TextBox3.Text Then
MessageBox.Show("User is already in database!!!", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning)
GoTo ErrorReturn 'execute difference piece of code
End If
End If
End If
Next

