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.
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:
For Each item As ListViewItem In myListView.Items
If CInt(item.Tag) = 2 Then
item.Selected = True
Exit For
End If
Next
If you would want to deselect any other items that might already be selected then you can do this:
vb.net Code:
For Each item As ListViewItem In myListView.Items
item.Selected = (CInt(item.Tag) = 2)
Next
Note that both these code snippets rely on the fact that all items have an Integer assigned to their Tag property.
Re: selecting an item in vb.net ListView
Thanks a lot jmcilhinney. It works.