• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2192
  • Last Modified:

VB.NET: listView find item (exact string)

Hi X-perts,

I need a fast code to find an item in ListView (without looping, if possible). I am using the following code now. The problem is that FindItemWithText always finds the 1st substring occurrence, but not the exact match.

any solutions that wouldn't imply looping throughout the listview items?

Thanks
For Each f In rc.Fields
                    Try
                        j = Me.ListView1.FindItemWithText(f.Name).Index
                        If (j <> -1) Then Me.ListView1.Items(j).Checked = True
                    Catch ex As Exception
                    End Try
                Next

Open in new window

0
andy7789
Asked:
andy7789
1 Solution
 
HainKurtSr. System AnalystCommented:
sort it first, so it finds exact match first ;)
0
 
funwithdotnetCommented:
I'm pretty sure the .FindItemWithText method is looping, so there won't be a performance hit by looping manually to find the correct item.

For a simple ListView, I would likely loop thru the items collection by index. Something like:
Dim x as Integer
For x = 0 to Me.ListView1.Items.Count -1
  If Me.ListView1.Items(x).Text = f.Name
    Me.ListView1.Items(x).Checked = True
    Exit For
  End If
Next

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now