Page 1 of 1

Search in listview ( vb.com )

Posted: Sun Feb 02, 2014 6:00 pm
by 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

Re: Search in listview ( vb.com )

Posted: Sun Feb 02, 2014 8:54 pm
by 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! :)

Re: Search in listview ( vb.com )

Posted: Mon Feb 03, 2014 12:06 am
by 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.

Re: Search in listview ( vb.com )

Posted: Mon Feb 03, 2014 10:45 am
by 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;

Re: Search in listview ( vb.com )

Posted: Mon Feb 03, 2014 12:05 pm
by 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.

Re: Search in listview ( vb.com )

Posted: Mon Feb 03, 2014 1:07 pm
by 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: