Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
for searching The data in listview I using : 2 Textbox, 1 button and 1 listview.

I need search more than 1 item, because i have same name and same value in my listview Columns.
This code just can search 1 item:

VB
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LviHK.MultiSelect = False
        LviHK.FullRowSelect = True

        Dim checkInt As Integer = FindItem(LviHK, TextBox1.Text)
        If checkInt <> -1 Then
            LviHK.Items(checkInt).Selected = True
            LviHK.Focus()
            LviHK.SelectedItems(0).EnsureVisible()
        Else
            Label3.Text = "Search string not found"
        End If
    End Sub
    Private Function FindItem(ByVal LV As ListView, ByVal TextToFind As String) As Integer
        ' Loop through LV’s ListViewItems.
        For i As Integer = 0 To LV.Items.Count - 1
            If Trim(LV.Items(i).Text) = Trim(TextToFind) Then
                ' If found, return the row number
                Return (i)
            End If
            For subitem As Integer = 0 To LV.Items(i).SubItems.Count - 1
                If Trim(LV.Items(i).SubItems(subitem).Text) = Trim(TextToFind) Then
                    ' If found, return the row number
                    Return (i)
                End If
            Next
        Next
        Return -1
    End Function
Posted

1 solution

Have a look at your function:
VB
Private Function FindItem(ByVal LV As ListView, ByVal TextToFind As String) As Integer
    ' Loop through LV’s ListViewItems.
    For i As Integer = 0 To LV.Items.Count - 1
        If Trim(LV.Items(i).Text) = Trim(TextToFind) Then
            ' If found, return the row number
            Return (i) 'returns only 1 value but need to return more than one
        End If
        For subitem As Integer = 0 To LV.Items(i).SubItems.Count - 1
            If Trim(LV.Items(i).SubItems(subitem).Text) = Trim(TextToFind) Then
                ' If found, return the row number
                Return (i) 'returns only 1 value but need to return more than one
            End If
        Next
    Next
    Return -1 'not found, OK, only 1 value
End Function


Use ListOf(T)[^] generic class.

VB
Function FindItem(ByVal LV As ListView, ByVal TextToFind As String) AS List(Of Integer)
Dim IntList As New List(Of Integer)

'inside loop
IntList.Add(i)

'after all
Return IntList
End 


You need to change code to your needs.
 
Share this answer
 
Comments
Yazid Aura Robbani 12-Apr-13 7:10am    
Nice Answer
I've been solved with your solution
Thank you Maciej Los
Maciej Los 12-Apr-13 11:43am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900