VB.NET: listView find item (exact string)

Posted on 2009-12-21
Last Modified: 2013-12-26
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?

For Each f In rc.Fields


                        j = Me.ListView1.FindItemWithText(f.Name).Index

                        If (j <> -1) Then Me.ListView1.Items(j).Checked = True

                    Catch ex As Exception

                    End Try


Open in new window

Question by:andy7789
    LVL 51

    Accepted Solution

    sort it first, so it finds exact match first ;)
    LVL 12

    Expert Comment

    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

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now