search a textbox for a key word in a Listview. VB.net

RobertoFreemano
RobertoFreemano used Ask the Experts™
on
Hi Experts,

I'm searching for a solution to search a textbox for a key word in a Listview. (winform)...  i.e. (you type Dog into the textbox and it finds dog in the Listview).

I found a solution on EE; however, it cannot get it to work :(

solution:
--------------------------------------------------------------------------------------------------------------
Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.TextChanged
        Dim i, strLength As Integer
        Dim searchStr As String
        searchStr = textbox1.Text
        strLength = searchStr.Length
        For i = 0 To listview1.ListItems.Count - 1
           If InStr(1, listview1.ListItems(i).Text, Text1.Text) > 0 Then
                listview1.ListItems(i).Selected = True
                listview1.ListItems(i).EnsureVisible
            End If
        Next i
--------------------------------------------------------------------------------------------------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I'd do a Trim() [or is it Strip()] on the input string to make sure there are no extraneous blanks getting passed.

If your index base is 0, the first parm to InStr() should be 0.
Retired
Distinguished Expert 2017
Commented:
Hi RobertoFreemano;

This should work. Note two versions, first is case insensitive and second case sensitive.

Case Insensitive:

    Private Sub TextBox1_TextChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim i, strLength As Integer
        strLength = TextBox1.Text.Length
        ListView1.SelectedItems.Clear()
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Text.ToUpper.StartsWith(TextBox1.Text.ToUpper) Then
                ListView1.Items(i).Selected = True
                ListView1.Items(i).EnsureVisible()
            End If
        Next
    End Sub

Case Sensitive:

    Private Sub TextBox1_TextChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim i, strLength As Integer
        strLength = TextBox1.Text.Length
        ListView1.SelectedItems.Clear()
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Text.StartsWith(TextBox1.Text) Then
                ListView1.Items(i).Selected = True
                ListView1.Items(i).EnsureVisible()
            End If
        Next
    End Sub

Fernando

Author

Commented:
Hi Fernando,

I pasted your solution into the textbox1 code as shown above... once ran...I type into the box, but noting happends?

1 = winform, 1 = textbox1, 1 = Listview

Any ideas?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi cookre,

I don't really understand your advice... I'm a bit of a novice. Can you A.B.C it for me please?

Cheers,
Roberto
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Roberto;

Make sure that the ListView property HideSelection is set to False otherwise it will not show up.

Fernando

Author

Commented:
That did the Trick,

Many thanks Fernando,

and thank you cookre for your rapid response

:)
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem, glad I was able to help. ;=)

Author

Commented:
Hi Fernando,

one last question if i may?

When I type into the textbox1... it highlights all the letters starting from (e.g.) = A, then filters down the more you type...

Q: is there a way of stopping it from highlighting all the letters until the final word is typed? So if A is typed, it only highlights the 1st letter A, then filters rather than highlight all the A's in the column?

Cheers,
Roberto
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Roberto;

Yes it will. The ListView control is unlike the TextBox control where you may highlight part of the text on the UI. The ListView control has a Selected property and either the item is selected or not.

Now that does not mean that you can not create a class that Inherits from the ListView control and draw the control on the UI the way you want it to, but if you ask me that is allot of work .

Fernando

Author

Commented:
No problem; again, many thanks for all your Help.

:)
Fernando SotoRetired
Distinguished Expert 2017

Commented:
No problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial