Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight selected word in Listview

Status
Not open for further replies.

Ajb2528

Technical User
Feb 22, 2002
270
GB
Hi!!

I wondered if there is any way to highlight a single word that is contained in a item/subitem within a listview.

I have implemented a search routine within a listview and I would like the user to see the word that was searched for highlighted within the item/subitem that it was found in.

Has anyone got any ideas on how to go about this?

Regards,

Alan
 
Ajb2528,

Try something like this
Code:
Private Sub SearchListView(ByVal sSearchString As String)
        LV1.MultiSelect = True ' Allow multiple finds
        LV1.HideSelection = False ' Ensure it highlights all finds

        Dim iCountItems As Integer
        Dim iCountSubItems As Integer
        Dim iFound As Integer

        For iCountItems = 1 To LV1.Items.Count
            ' First Column 1..
            If LV1.Items(iCountItems).Text.IndexOf(sSearchString) > 0 Then
                ' If a match found ..
                LV1.Items(iCountItems).Selected = True ' Highlight it
                LV1.Items(iCountItems).EnsureVisible() ' Scroll to it if necessary
                iFound += 1
            End If

            ' Now the SubItems..
            For iCountSubItems = 1 To LV1.Items(iCountItems).SubItems.Count
                If LV1.Items(iCountItems).SubItems(iCountSubItems).Text.IndexOf(sSearchString) > 0 Then
                    LV1.Items(iCountItems).Selected = True
                    LV1.Items(iCountItems).EnsureVisible()
                    iFound += 1
                    Exit For ' If you have found one, no need to keep searching further subitems
                End If
            Next iCountSubItems

        Next iCountItems

        ' None found
        If iFound = 0 Then MsgBox("Sorry, no matches found", vbOKOnly, "Search")

    End Sub

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
PsychoCoder,

Thanks for the response but what I really wanted was to highlight the word or phrase within the item/subitem, not the whole of the item/subitem.

Regards,

Alan
 
Ajb2528,

Unless I'm incorrect (or unless you create a custom control taking control of the Paint Event) that this is not possible with a ListView.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Ok - Thanks for trying to help!

Regards,

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top