[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
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.
Re: Searching an item in Listview and highlighting matching row
Re: Searching an item in Listview and highlighting matching row
Quote:
Originally Posted by
bjay_tiamsic26
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. :thumb:
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
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.
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?
Re: Searching an item in Listview and highlighting matching row
Quote:
Originally Posted by
bjay_tiamsic26
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.
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
Re: Searching an item in Listview and highlighting matching row
I just tested this and it seems to do the job:
vb.net Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim searchText = Me.TextBox1.Text
If searchText = String.Empty Then
'Don't bother searching if there's no text to search for.
MessageBox.Show("Please enter search text.")
Else
Dim startIndex = 0
Dim item As ListViewItem = Nothing
'If one item is selected and it already matches the search text, start searching from the next item.
'Otherwise, start searching from the beginning.
If Me.ListView1.SelectedItems.Count = 1 AndAlso Me.ListView1.SelectedItems(0).Text = searchText Then
startIndex = Me.ListView1.SelectedIndices(0) + 1
End If
'Don't search if we're already at the end of the items.
If startIndex < Me.ListView1.Items.Count Then
Do
'Find the first partial match.
item = Me.ListView1.FindItemWithText(searchText, False, startIndex)
If item Is Nothing OrElse item.Text = searchText Then
'There is no partial match or we have already found a full match.
Exit Do
End If
'Search again from the item after the last partial match.
startIndex = item.Index + 1
'Stop searching if we're at the end of the items.
If startIndex >= Me.ListView1.Items.Count Then
Exit Do
End If
Loop
End If
'Clear the current selection.
Me.ListView1.SelectedItems.Clear()
If item Is Nothing Then
MessageBox.Show("No match found.")
Else
'Select the matching item.
item.Selected = True
item.EnsureVisible()
Me.ListView1.Select()
End If
End If
End Sub
End Class
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. :bigyello:
Re: Searching an item in Listview and highlighting matching row
I have updated the code with some extra comments.
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?
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.
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
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.
Re: Searching an item in Listview and highlighting matching row
Re: Searching an item in Listview and highlighting matching row
Thank you for another problem solved!
God bless jmcilhinney! :wave::cool: