Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0 VB9.0 VB10.0 VB.NET
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:
 
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 11-Apr-13 7:05am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have a look at your function:
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.
 
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.
  Permalink  
Comments
Yazid Aura Robbani at 12-Apr-13 7:10am
   
Nice Answer
I've been solved with your solution
Thank you Maciej Los
Maciej Los at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100