使用文本查找项
-
创建一个 ListView, View 属性设置为 Details 或 List ,然后用项填充该 ListView。
-
调用 FindItemWithText 方法,向其传递要查找的项的文本。
-
下面的代码示例演示如何创建基本 ListView,用项进行填充并使用由用户输入的文本来在列表中查找项
Private textListView As New ListView()
Private WithEvents searchBox As New TextBox()
Private Sub InitializeTextSearchListView()
searchBox.Location = New Point(150, 20)
textListView.Scrollable = True
textListView.Width = 80
textListView.Height = 50
' Set the View to list to use the FindItemWithText method.
textListView.View = View.List
' Populate the ListView with items.
textListView.Items.AddRange(New ListViewItem() { _
New ListViewItem("Amy Alberts"), _
New ListViewItem("Amy Recker"), _
New ListViewItem("Erin Hagens"), _
New ListViewItem("Barry Johnson"), _
New ListViewItem("Jay Hamlin"), _
New ListViewItem("Brian Valentine"), _
New ListViewItem("Brian Welker"), _
New ListViewItem("Daniel Weisman")})
' Add the controls to the form.
Me.Controls.Add(textListView)
Me.Controls.Add(searchBox)
End Sub
Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles searchBox.TextChanged
' Call FindItemWithText with the contents of the textbox.
Dim foundItem As ListViewItem = _
textListView.FindItemWithText(searchBox.Text, False, 0, True)
If Not (foundItem Is Nothing) Then
textListView.TopItem = foundItem
End If
End Sub