.net 尝试在listview中搜索列并返回结果
我正在从指定的索引中搜索我的listview,并返回单词的第一个结果(项目所在的索引)。除了我只能搜索listview中的第一列之外,它工作得很好。如何仅搜索第二列.net 尝试在listview中搜索列并返回结果,.net,vb.net,string,visual-studio-2010,listview,.net,Vb.net,String,Visual Studio 2010,Listview,我正在从指定的索引中搜索我的listview,并返回单词的第一个结果(项目所在的索引)。除了我只能搜索listview中的第一列之外,它工作得很好。如何仅搜索第二列 Private Function FindLogic(ByVal LV As ListView, ByVal CIndex As Integer, ByVal SearchFor As String) As Integer Dim idx As Integer Dim It = From i In LV.Items Where i.
Private Function FindLogic(ByVal LV As ListView, ByVal CIndex As Integer, ByVal SearchFor As String) As Integer
Dim idx As Integer
Dim It = From i In LV.Items Where i.index > CIndex And i.Text = SearchFor
If It.Count > 0 Then
idx = It(0).Index
Else
idx = -1
End If
Return idx
End Function
我已经弄明白了。此解决方案允许您选择指定的索引开始搜索,并检查索引为1的列(通常是第二列) 您还可以向函数中添加另一个参数,以便选择不同的列来检查字符串,如下所示:
Private Function FindLogic(ByVal LV As ListView, ByVal CIndex As Integer, ByVal Column As Integer, ByVal SearchFor As String) As Integer
Dim idx As Integer
Dim It = From i In LV.Items Where i.index > CIndex And i.SubItems(Column).Text = SearchFor
If It.Count > 0 Then
idx = It(0).Index
Else
idx = -1
End If
Return idx
End Function
要使用此函数,它将如下所示:
FindLogic(Listview1, 1, 1, "Dog")
FindLogic(Listview1, 1, LV.SelectedIndices(0), "Dog")
您也可以让它从所选项目进行搜索,如下所示:
FindLogic(Listview1, 1, 1, "Dog")
FindLogic(Listview1, 1, LV.SelectedIndices(0), "Dog")
我已经弄明白了。此解决方案允许您选择指定的索引开始搜索,并检查索引为1的列(通常是第二列) 您还可以向函数中添加另一个参数,以便选择不同的列来检查字符串,如下所示:
Private Function FindLogic(ByVal LV As ListView, ByVal CIndex As Integer, ByVal Column As Integer, ByVal SearchFor As String) As Integer
Dim idx As Integer
Dim It = From i In LV.Items Where i.index > CIndex And i.SubItems(Column).Text = SearchFor
If It.Count > 0 Then
idx = It(0).Index
Else
idx = -1
End If
Return idx
End Function
要使用此函数,它将如下所示:
FindLogic(Listview1, 1, 1, "Dog")
FindLogic(Listview1, 1, LV.SelectedIndices(0), "Dog")
您也可以让它从所选项目进行搜索,如下所示:
FindLogic(Listview1, 1, 1, "Dog")
FindLogic(Listview1, 1, LV.SelectedIndices(0), "Dog")
您需要访问ListViewItem的ListViewSubItems以获取其他列。使用这些子项,您将能够通过索引搜索不同的列,而不仅仅是搜索文本。您可以简单地在循环中使用循环来执行搜索。如果愿意,您可以显式搜索,因为您知道您只想搜索第二列,但是通过在循环中使用循环,您实际上可以搜索任何列。以下是一个示例:
Dim idx As Integer = -1
For Each lvi As ListViewItem In Me.lvwData.Items
If lvi.SubItems(1).Text = "TextToSearchFor" Then
idx = lvi.Index
End If
Next
您需要访问ListViewItem的ListViewSubItems以获取其他列。使用这些子项,您将能够通过索引搜索不同的列,而不仅仅是搜索文本。您可以简单地在循环中使用循环来执行搜索。如果愿意,您可以显式搜索,因为您知道您只想搜索第二列,但是通过在循环中使用循环,您实际上可以搜索任何列。以下是一个示例:
Dim idx As Integer = -1
For Each lvi As ListViewItem In Me.lvwData.Items
If lvi.SubItems(1).Text = "TextToSearchFor" Then
idx = lvi.Index
End If
Next
我实际上是在尝试从指定的索引中搜索,然后搜索第一个实例并返回它的索引。因此,我可以在第5行开始搜索,并返回指定单词的第一个实例索引。看起来您已经找到了答案。键是访问子项,而不是ListViewItem的Text属性。我实际上是在尝试从指定的索引中搜索第一个实例并返回其索引。因此,我可以在第5行开始搜索,并返回指定单词的第一个实例索引。看起来您已经找到了答案。该键访问的是子项,而不是ListViewItem的Text属性。