12,068,520 members (66,985 online)
Rate this:
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:

```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
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

Rate this:

## 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
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

'after all
Return IntList
End ```

You need to change code to your needs.
Yazid Aura Robbani 12-Apr-13 7:10am

I've been solved with your solution
Thank you Maciej Los
Maciej Los 12-Apr-13 11:43am

You're welcome ;)

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 290 CHill60 239 OriginalGriff 204 CPallini 202 Richard Deeming 184
 Dave Kreskowiak 2,126 OriginalGriff 1,501 Richard MacCutchan 1,455 CPallini 1,042 Sergey Alexandrovich Kryukov 992