Guys, i have problem in my search button after i search the name it didn't highlight the name of the person.

this is the code that i found on the internet.

Dim searchName As New TextBox
        Dim sname As String
        sname = InputBox("Search for gym user." & searchName.Text)

        Dim foundItem As ListViewItem = _
        ListView1.FindItemWithText(searchName.Text, True, 1, True)


        If foundItem IsNot Nothing Then
            ListView1.MultiSelect = False
            ListView1.TopItem = foundItem

            foundItem.Selected = True
        End If
5 Years
Discussion Span
Last Post by LuckyBhumkar

See if this helps.

With ListView1 '// shorten code.
            If Not .Items.Count = 0 Then '// check if items in ListView.
                Dim sNameSearch As String = InputBox("Type in the name of the member to search for.", "Search for gym member")
                '// search ListView for item.
                Dim foundItem As ListViewItem = .FindItemWithText(sNameSearch, True, 0, False)
                '// pressing Cancel in InputBox will return "" as a value.
                If Not sNameSearch = "" Then '// if not Cancel or not an empty value.
                    If foundItem IsNot Nothing Then '// if item has been found.
                        .MultiSelect = False '// disable MultiSelect to only select the found item.
                        .TopItem = foundItem '// bring item to top of ListView without having to scroll.
                        foundItem.Selected = True '// select item to highlight.
                        .MultiSelect = True  '// enable MultiSelect again if needed.
                        .Select() '// .Select ListView to display the highlighted item.
                    End If
                End If
                MsgBox("cannot search for something if it isn't there", MsgBoxStyle.Information) '// if no items in ListView.
            End If
        End With

Not sure if you are aware that using "1" as Start Index, will start the search at the second item added in the ListView.


Thanks, it's working!

Dim founditem As ListViewItem = LV2.FindItemWithText(SID.Text, False, 0, False)
            If founditem IsNot Nothing Then
                LV2.TopItem = founditem
                founditem.Selected = True
            End If

Edited by Ezzaral: Snipped 'fake sig' link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.