2012-11-26 45 views
1

我正在从指定索引搜索我的列表视图并返回单词的第一个结果(项目所在的索引)。除了我只能在列表视图中搜索第一列以外,它的效果很好。我如何只搜索第二列?试图在列表视图中搜索列并返回结果

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 

回答

0

我已经想通了。该解决方案允许您选择指定索引开始从该搜索和检查与列和1指数(通常是你的第二列)

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.SubItems(1).Text = SearchFor 
    If It.Count > 0 Then 
     idx = It(0).Index 
    Else 
     idx = -1 
    End If 
    Return idx 
    End Function 

您还可以添加其他参数为,所以你的功能可以选择不同的列来检查字符串,像这样:

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") 
1

你要访问的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 
+0

我实际上是试图从指定的索引搜索并搜索第一个实例并返回它的索引。所以我可以在第5行开始搜索并返回指定单词的第一个实例索引。 – user1632018

+0

无论如何,它看起来像你想出来的。关键是访问SubItems而不是ListViewItem的Text属性。 – Wooster11