[英]searching listview in vb.net one-by-one
I am trying to search through items in listview control in vb.net one by one. 我正在尝试在vb.net的listview控件中逐项搜索项目。 ie When the user types in some keyworrk in the textbox and presses enter, the first found item in the listview should get highlighted, when he presses enter again, the second search item in the listview should get highlighted and so on.
即,当用户在文本框中键入某些按键问题并按Enter键时,列表视图中的第一个找到的项目应突出显示,当他再次按Enter键时,列表视图中的第二个搜索项应被突出显示,依此类推。
I am not fully aware of how to keep track of already found items while searching. 我不完全了解如何在搜索时跟踪已找到的项目。 As of now , I am searching all the items in the listbx using the code :
到目前为止,我正在使用以下代码搜索listbx中的所有项目:
If (e.KeyCode = Keys.Enter) Then
'START
ListView1.BeginUpdate()
ListView1.SelectedIndices.Clear()
If TextBox1.Text.Length > 0 Then
Dim lstOfStrings() As String = TextBox1.Text.Split(","c)
For Each s As String In lstOfStrings
For index As Integer = 0 To ListView1.Items.Count - 1
If s.Trim() <> "" Then
Dim item As String = ListView1.Items(index).ToString()
If item.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
'ListView1.SelectedIndices.Add(index)
ListView1.Items(index).BackColor = Color.LightPink
End If
End If
Next
Next s
End If
ListView1.EndUpdate()
End if
You have several options, I believe 我相信您有几种选择
As for a sample for #3 - what this means is that you would have a variable, outside the method for example - 对于#3的示例-这意味着您将在方法之外拥有一个变量,例如-
Dim highlightedItem as String
In your loop, where you currently change the background colour, you would also set this variable; 在您当前更改背景颜色的循环中,您还将设置此变量; if, for example, you wanted to use the same value you use for comparison (I assume it's unique), you could use
例如,如果您想使用用于比较的相同值(我认为它是唯一的),则可以使用
highlightedItem = item
In your loop you will need to ignore any entry before you reach the currently highlighted item, to do that declare boolean variable highlightedItemFound OUTSIDE the loop and then inside the loop, after extracting item, add something like 在循环中,您需要先忽略任何条目,然后才能到达当前突出显示的项目,为此,请在循环外声明布尔变量highlightedItemFound,然后在循环中,在提取项目之后,添加以下内容
If not highlightedItemFound and item=highlightedItem then
highlightedItemFound = true
continue
end if
This will ensure that your check will only start applying after the highlighted item is reached in the list. 这将确保您的支票仅在列表中达到突出显示的项目后才开始应用。
You will also need to remove the highlight from the previously selected one, but as you will have a reference to it in the variable you can do it as you loop as well. 您还需要从以前选择的突出显示中删除突出显示,但是由于您将在变量中对其进行引用,因此您也可以在循环时执行突出显示。
This does exactly what you want: Just call FindInListView function when the user press enter, the same function can be used with more than one listview! 这正是您想要的功能:只需在用户按下Enter键时调用FindInListView函数,同一函数就可以用于多个listview! I added also a "FindInGrid" function it does exactly the same but with DataGrids.
我还添加了“ FindInGrid”功能,但与DataGrids完全相同。
Private DicPos As New Dictionary(Of Integer, Integer)
Public Function FindInListView(ByVal lv As ListView, ByVal Texto As String) As Integer
If Not DicPos.ContainsKey(lv.Handle) Then
DicPos.Add(lv.Handle, -1)
End If
If lv.Items.Count = 0 Then Return 0
Texto = Texto.Trim.ToUpper
If Texto.Length = 0 Then lv.Items(0).Selected = True
Dim Itm As ListViewItem
For x As Integer = DicPos(lv.Handle) + 1 To lv.Items.Count - 1
Itm = lv.Items(x)
For Each cell As ListViewItem.ListViewSubItem In Itm.SubItems
If cell.Text.ToUpper.Contains(Texto) Then
lv.Items(x).Selected = True
Try
lv.BindingContext.Item(lv.DataBindings).Position = x
Catch ex As Exception
End Try
DicPos(lv.Handle) = Itm.Index
Return DicPos(lv.Handle)
End If
Next
Next
If DicPos(lv.Handle) = -1 Then
lv.BindingContext.Item(lv.DataBindings).Position = 0
Return DicPos(lv.Handle)
Else
DicPos(lv.Handle) = -1
Return FindInListView(lv, Texto)
End If
End Function
Public Function FindInGrid(ByVal Grid As DataGridView, ByVal Texto As String, ByVal Position As Integer) As Integer
Texto = Texto.Trim.ToUpper
If Texto.Length = 0 Then
Grid.BindingContext.Item(Grid.DataSource).Position = 0
Return 0
End If
For x As Integer = Position To Grid.Rows.Count - 1
For Each Cel As DataGridViewCell In Grid.Rows(x).Cells
If Cel.Value.ToString.ToUpper.Contains(Texto) Then
Grid.Rows(Cel.RowIndex).Selected = True
Try
Grid.BindingContext.Item(Grid.DataSource).Position = x
Catch ex As Exception
End Try
Return Cel.RowIndex
End If
Next
Next
If Position = 0 Then
Grid.BindingContext.Item(Grid.DataSource).Position = 0
Return 0
Else
Return FindInGrid(Grid, Texto, 0)
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.