Search in listview ( vb.com )

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
6 posts Page 1 of 1
Contributors
User avatar
troop
Dedicated Member
Dedicated Member
Posts: 75
Joined: Sat Sep 28, 2013 11:57 am

Search in listview ( vb.com )
troop
idk if thats search or find, but i want a user to type in a textbox something then he press Search it will highlight the item/items (if possible) that is the same as textbox

IF possible to even check the WHOLE sub items, not just the first, cuz this has 4 coloums in total so if thats possible please tell, my current code doesn't even highlight idk if it does scroll a bit but doesnt make the real find function
Code: Select all
     Dim foundItem As ListViewItem = _
           ListView1.FindItemWithText(txtFind.Text, False, 0, True)

                    If (foundItem IsNot Nothing) Then
                        ListView1.TopItem = foundItem
                    End If
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Search in listview ( vb.com )
smashapps
I don't remember where this code came from but I used this in my Animal Crossing program to search for items in a Listbox.
Code: Select all
Public Sub SearchCharacters(ByVal searchTerm As String)
        Try
            For Each lbItem As Object In lstCharacters.Items
                If lbItem.ToString = searchTerm Then
                    lstCharacters.SelectedItem = lbItem
                End If
            Next
        Catch ex As Exception

        End Try
       

    End Sub
Then you need to add this code below to the button that will search for the term the user inputs.
Code: Select all
  If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            Dim s As String = txtSearchCharacter.Text
            Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
            txtSearchCharacter.Text = s2
            SearchCharacters(s2)
        End If
Put that on the Textbox KeyDown event so the user doen't have to click the button, but can click Enter.

Code for the search button:
Code: Select all
            Dim s As String = txtSearchCharacter.Text
            Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
            txtSearchCharacter.Text = s2
            SearchCharacters(s2)
Searching for whaever term the user inputs is case sensitive, and the code will select the item it finds in the listbox, if nothing was found, nothing will be selected.

Goodluck! :)
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Search in listview ( vb.com )
mandai
You can use this code to search in listview sub items:
Code: Select all
    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click

        ListView1.SelectedIndices.Clear()
        ListView1.HideSelection = False

        Dim found As List(Of Integer) = New List(Of Integer)

        For row As Integer = 0 To ListView1.Items.Count - 1
            For column As Integer = 0 To ListView1.Items(row).SubItems.Count - 1

                If ListView1.Items(row).SubItems(column).Text.Contains(txtSearch.Text) Then

                    found.Add(row)
                    Exit For
                End If
            Next

        Next

        For i As Integer = 0 To found.Count - 1

            ListView1.SelectedIndices.Add(found(i))
        Next

    End Sub
The user just needs to click btnSearch after typing in the text.
User avatar
troop
Dedicated Member
Dedicated Member
Posts: 75
Joined: Sat Sep 28, 2013 11:57 am

Re: Search in listview ( vb.com )
troop
@smashapps, iknew about keydown and the enter thing but what does
Code: Select all
  e.SuppressKeyPress = True
mean

@mandai, thanks for this code i will soon use it not now cuz i have fever cryer;
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Search in listview ( vb.com )
smashapps
Gets or sets a value indicating whether the key event should be passed on to the underlying control.
I use that to prevent the annoying ding windows makes when you press keys on your form.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
troop
Dedicated Member
Dedicated Member
Posts: 75
Joined: Sat Sep 28, 2013 11:57 am

Re: Search in listview ( vb.com )
troop
smashapps wrote:
Gets or sets a value indicating whether the key event should be passed on to the underlying control.
I use that to prevent the annoying ding windows makes when you press keys on your form.
lol nice :D :lol:
6 posts Page 1 of 1
Return to “Coding Help & Support”