Results 1 to 18 of 18

Thread: [RESOLVED] Searching an item in Listview and highlighting matching row

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Resolved [RESOLVED] Searching an item in Listview and highlighting matching row

    Hello. I have a listview in my vb.net program and a textbox on a form.
    If the user enters a text in runtime and clicks on Enter key,
    the program should find the first row and highlight it, then if user clicks enter for the second time, the next matching row must be highlighted next.

    The text should find in column 0 (first column)

    Any help would be very much appreciated

    thanks and have a nice day!
    Bob

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    You can simply call the FindItemWithText method. It will return the first matching item and you can then set the Selected property of that item. You can also specify a start index so, if an item is already selected, you can specify the next index as the place to start.

    Note that FindItemWithText finds the first item that starts with the specified text. If you want only an exact match then you have to test the Text of the returned item for an exact match yourself. If it doesn't match then you'd call the method again immediately.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    Alright. I will try

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    Quote Originally Posted by bjay_tiamsic26 View Post
    Alright. I will try
    That's all I ask. If you have issues then by all means post back and show us what you've done but it's good to see that you want to give it a go where so many others ask for "an example" without expending any effort first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    here's what I have so far.
    but when I try to press enter again, the first rowindex that matched first is also highlighted. therefore the 1st and 2nd row are highlighted



    Code:
            If Asc(e.KeyChar) = 13 Then
                Dim itmX As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, selectitemIndex)
    
                If Not itmX Is Nothing Then
                    ListView1.Focus()
                    itmX.Selected = True
                    ListView1.Items(itmX.Index).Selected = True
                    selectitemIndex = itmX.Index + 1
                    itmX.EnsureVisible()
    
                End If
    
            End If

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    A ListView allows multiple items to be selected at a time so selecting one will not automatically deselect another. You'll need to deselect any currently selected item(s) yourself. The easiest way to do that would be to call Clear on the SelectedItems collection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    how can I keep the highlighted row in listview when the focus is turned in to the textbox?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    Quote Originally Posted by bjay_tiamsic26 View Post
    how can I keep the highlighted row in listview when the focus is turned in to the textbox?
    Items don't stop being selected when the ListView loses focus but the highlight colour is removed. You can set the HideSelection property to False to keep the highlight even when the ListView does not have focus, although the colour will change. It will look just like the list view on the right-hand side of Windows Explorer, with a fainter highlight when not in focus.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    Can you check what I did and suggest for improvement.
    I also noticed that the Messagebox pops up before highlighting the last matched row though Listview1.Items(itmX.Index).Selected = True comes first before the messagebox coding.


    Code:
    If Asc(e.KeyChar) = 13 Then
                ListView1.SelectedItems.Clear()
                
                Dim itmX As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, selectitemIndex)
    
    
                If Not itmX Is Nothing Then
                    ListView1.Focus()
                    itmX.Selected = True
                    ListView1.Items(itmX.Index).Selected = True
                    itmX.EnsureVisible()
    
                End If
    
                If selectitemIndex = ListView1.Items.Count - 1 Then
                    MsgBox("No more match found")
                    Exit Sub
                End If
                selectitemIndex = itmX.Index + 1
            End If

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    I just tested this and it seems to do the job:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Dim searchText = Me.TextBox1.Text
    5.  
    6.         If searchText = String.Empty Then
    7.             'Don't bother searching if there's no text to search for.
    8.             MessageBox.Show("Please enter search text.")
    9.         Else
    10.             Dim startIndex = 0
    11.             Dim item As ListViewItem = Nothing
    12.  
    13.             'If one item is selected and it already matches the search text, start searching from the next item.
    14.             'Otherwise, start searching from the beginning.
    15.             If Me.ListView1.SelectedItems.Count = 1 AndAlso Me.ListView1.SelectedItems(0).Text = searchText Then
    16.                 startIndex = Me.ListView1.SelectedIndices(0) + 1
    17.             End If
    18.  
    19.             'Don't search if we're already at the end of the items.
    20.             If startIndex < Me.ListView1.Items.Count Then
    21.                 Do
    22.                     'Find the first partial match.
    23.                     item = Me.ListView1.FindItemWithText(searchText, False, startIndex)
    24.  
    25.                     If item Is Nothing OrElse item.Text = searchText Then
    26.                         'There is no partial match or we have already found a full match.
    27.                         Exit Do
    28.                     End If
    29.  
    30.                     'Search again from the item after the last partial match.
    31.                     startIndex = item.Index + 1
    32.  
    33.                     'Stop searching if we're at the end of the items.
    34.                     If startIndex >= Me.ListView1.Items.Count Then
    35.                         Exit Do
    36.                     End If
    37.                 Loop
    38.             End If
    39.  
    40.             'Clear the current selection.
    41.             Me.ListView1.SelectedItems.Clear()
    42.  
    43.             If item Is Nothing Then
    44.                 MessageBox.Show("No match found.")
    45.             Else
    46.                 'Select the matching item.
    47.                 item.Selected = True
    48.                 item.EnsureVisible()
    49.                 Me.ListView1.Select()
    50.             End If
    51.         End If
    52.     End Sub
    53.  
    54. End Class
    Last edited by jmcilhinney; Jan 29th, 2014 at 01:33 AM. Reason: Updated code comments.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    Thank you for this. I will analyze the code and see what each line returns or means then Ill be posting back if I have a question.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    I have updated the code with some extra comments.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    If the first matching text is in the 3rd row, why is the SelectedIndeces in Line 16 returns 2? is it the first two rows that have been skipped?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    As with all .NET collections, the Items of a ListView is zero-based. The first item is at index 0 so the third item is at index 2.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    I got the point already, sorry my mistaked. It returned 2 on the second time the button is clicked because rowindex=2 has been selected already on the first click

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    and

    Code:
    item = Me.ListView1.FindItemWithText(searchText, False, startIndex)
    finds the first matching text after the startindex

    that's why

    item.Index equals to 2 because the first matching criteria was found on the 3rd row.

    Please correct me if my interpretation in incorrect.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Searching an item in Listview and highlighting matching row

    That is correct.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Posts
    163

    Re: Searching an item in Listview and highlighting matching row

    Thank you for another problem solved!

    God bless jmcilhinney!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width