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)

        'ListView1.Focus()

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

            foundItem.Selected = True
        End If

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
            Else
                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.

Thank you sir. it worked!

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.