Finding an item in ListView Control

Hi experts

     How to find an item in ListViewItems and also in Subitems, if a text is known fully or partially.
                (that is simply a replacement for FindItem method in VB6 ListView Control)

Thanks
Shiju
LVL 14
Shiju SasidharanAssoc Project ManagerAsked:
Who is Participating?
 
RonaldBiemansConnect With a Mentor Commented:
That is no problem because the hashtable also holds the listview item, like


Dim myItems As New Hashtable

        For x As Integer = 0 To 10
            Dim y As New ListViewItem(x.ToString)
            myItems.Add(x.tostring, y)
            ListView1.Items.Add(y)
        Next

then you can cast them back to the listviewitem

like

directcast(myitems("1"),listviewitem).text = "whatever"
0
 
iboutchkineCommented:
Dim lv As ListViewItem
For Each lv In ListView1.Items
            If lv.Text = "MatchingString" Then
                lv.Selected = True
                ' MsgBox(lv.Index)
            End If
Next
0
 
RonaldBiemansCommented:
to find a part extend Iboutchkine code with

private sub FindItem(MySearchText as string, exactMatch as boolean)
For Each lv In ListView1.Items
        If exactMatch Then
            If itm.Text = mySearchText Then
               ' an exact match was found ...
            End If
         Else
            mySearchText &= "*"
            If itm.Text Like mySearchText Then
               ' a partial match was found ...
            End If
         End If
next
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
SanclerCommented:
and to extend Ronald's code to subitems

    Private Sub FindItem(ByVal MySearchText As String, ByVal exactMatch As Boolean)

        Dim itm As ListViewItem
        Dim subItm As ListViewItem.ListViewSubItem

        For Each itm In ListView1.Items
            If exactMatch Then
                If itm.Text = mySearchText Then
                    ' an exact match was found ...
                Else
                    For Each subItm In itm.SubItems
                        If subItm.Text = mySearchText Then
                            ' an exact match was found ...
                        End If
                    Next
                End If
            Else
                mySearchText &= "*"
                If itm.Text Like mySearchText Then
                    ' a partial match was found ...
                Else
                    For Each subItm In itm.SubItems
                        If subItm.Text Like mySearchText Then
                            ' a partial match was found ...
                        End If
                    Next
                End If
            End If
        Next

    End Sub

Roger
0
 
Shiju SasidharanAssoc Project ManagerAuthor Commented:
Hi iboutchkine , RonaldBiemans , Sancler
   thank u all for the quick response.
  well, is there any way otherthan looping?
0
 
iboutchkineCommented:
not that I know of
0
 
SanclerCommented:
I can't think of a way in the ListView itself.  What is the problem with looping?
0
 
SanclerConnect With a Mentor Commented:
Correction to the code posted earlier

    Private Sub FindItem(ByVal MySearchText As String, ByVal exactMatch As Boolean)

        Dim itm As ListViewItem
        Dim subItm As ListViewItem.ListViewSubItem

        If Not exactMatch Then
            MySearchText = "*" & MySearchText & "*"
        End If

        For Each itm In listView1.Items
            If exactMatch Then
                If itm.Text = MySearchText Then
                    ' an exact match was found ...
                Else
                    For Each subItm In itm.SubItems
                        If subItm.Text = MySearchText Then
                            ' an exact match was found ...
                        End If
                    Next
                End If
            Else
                If itm.Text Like MySearchText Then
                    ' a partial match was found ...
                Else
                    For Each subItm In itm.SubItems
                        If subItm.Text Like MySearchText Then
                            ' a partial match was found ...
                        End If
                    Next
                End If
            End If
        Next

    End Sub

Roger
0
 
RonaldBiemansCommented:
you could add the listitems to a hashtable and use the text as a key, that way you don't have to loop
0
 
SanclerCommented:
I wondered about that.  But if he does that, and locates the item in the hashtable, is there then any way from that to find it in the ListView without looping?
0
 
SanclerCommented:
Ronald

Neat.

I'd experimented with the general idea but not been able to get it to work because I was just putting the text in the hashtable rather than the ListViewItem.  Which was why I added "in the ListView itself" to "I can't think of a way".  But I have now got it working, in principle.

But, in the context of the question, I am still seeing problems.

1)      The hashtable keys have to be unique, but the ListViewItem.Text might not be - particularly when it is remembered that the question wants to search the Text of ListViewSubItems, too.

2)      I can't find a HashTable method to find a wildcard match.

Am I wrong?  Or are there workarounds?

Roger
0
 
RonaldBiemansCommented:
1. true and there is no workaround but to rename it (by added a number at the end for instance)
2. also true, there is no wildcard option (why should it, because it can only return 1 Item), you could however build your own collection and implement the wildcard option yourself
0
 
SanclerCommented:
If it were me, I'd stick with looping.
0
 
RonaldBiemansCommented:
I would too, unless the listview was very big.

0
 
Shiju SasidharanAssoc Project ManagerAuthor Commented:
That means for each subitem i need to keep seperate hashtables inorder to implement Subitem search.
0
 
SanclerCommented:
Yes, and then you would have to loop through the hashtables.  And there would still be the 'partial match' problem to solve.
0
 
RonaldBiemansCommented:
Hi shijusn, no you don't need different hashtables so you won't have to loop, since a hashtable consist of objects you can put a listitem aswell as a sublist item in there, you will have a problem with partial matches like Sancler said.
0
 
SanclerCommented:
>>
you can put a listitem aswell as a sublist item in there
<<

... provided all the .Text properties you want for the keys are (or you are prepared to doctor them to make them) unique
0
 
RonaldBiemansCommented:
True :-)
0
 
RonaldBiemansCommented:
But if there are listitems with the same name you would also have to adjust your loop (function) to incorporate duplicates
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.