向 ListView 控件添加搜索功能

使用文本查找项

  1. 创建一个 ListView, View 属性设置为 Details 或 List ,然后用项填充该 ListView。

  2. 调用 FindItemWithText 方法,向其传递要查找的项的文本。

  3. 下面的代码示例演示如何创建基本 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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值