[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding an item in ListView Control

Posted on 2005-05-10
21
Medium Priority
?
729 Views
Last Modified: 2007-10-18
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
0
Comment
Question by:Shiju Sasidharan
  • 8
  • 8
  • 2
  • +1
20 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13967282
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13967917
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
 
LVL 34

Expert Comment

by:Sancler
ID: 13969469
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 13969756
Hi iboutchkine , RonaldBiemans , Sancler
   thank u all for the quick response.
  well, is there any way otherthan looping?
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13969840
not that I know of
0
 
LVL 34

Expert Comment

by:Sancler
ID: 13971593
I can't think of a way in the ListView itself.  What is the problem with looping?
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 1000 total points
ID: 13971817
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13975692
you could add the listitems to a hashtable and use the text as a key, that way you don't have to loop
0
 
LVL 34

Expert Comment

by:Sancler
ID: 13975751
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
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 1000 total points
ID: 13975772
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
 
LVL 34

Expert Comment

by:Sancler
ID: 13976358
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13976391
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
 
LVL 34

Expert Comment

by:Sancler
ID: 13976423
If it were me, I'd stick with looping.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13976444
I would too, unless the listview was very big.

0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 14021031
That means for each subitem i need to keep seperate hashtables inorder to implement Subitem search.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14021374
Yes, and then you would have to loop through the hashtables.  And there would still be the 'partial match' problem to solve.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 14024908
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
 
LVL 34

Expert Comment

by:Sancler
ID: 14024951
>>
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 14024955
True :-)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 14024968
But if there are listitems with the same name you would also have to adjust your loop (function) to incorporate duplicates
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question