selecting an item in vb.net ListView-VBForums
Results 1 to 3 of 3

Thread: selecting an item in vb.net ListView

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    7

    selecting an item in vb.net ListView

    Hi everyone,

    I am using visual basic .net 2008.
    I have a problem selecting an item in a ListView.
    my problem is that i have set a value in the Tag property. Tag now contains the ID of the item.

    Suppose now the Tag of the ListView item contains the value '2'
    i want to select the Item in the ListView with the Tag=2

    how do i do that? Please help me.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    Re: selecting an item in vb.net ListView

    You'll need to loop through all the items and test the Tag of each one. When you find one that has the value 2 you select that item and exit the loop:
    vb.net Code:
    1. For Each item As ListViewItem In myListView.Items
    2.     If CInt(item.Tag) = 2 Then
    3.         item.Selected = True
    4.         Exit For
    5.     End If
    6. Next
    If you would want to deselect any other items that might already be selected then you can do this:
    vb.net Code:
    1. For Each item As ListViewItem In myListView.Items
    2.     item.Selected = (CInt(item.Tag) = 2)
    3. Next
    Note that both these code snippets rely on the fact that all items have an Integer assigned to their Tag property.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: selecting an item in vb.net ListView

    Thanks a lot jmcilhinney. It works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.