Getting the index of a ListView click.

haroldjclements

Freshman
Joined
Jun 13, 2004
Messages
46
Hello,

I am using a listView object to hold three columns of data pulled out of a database. What I want is the used to be able to click a line and the index and column data be available to be used within the next part of my program.

I know that the listBox object has a .get_SelectedIndex() method. However the listView does not have this method.

If anyone has any ideas I will be very grateful.

Thanks,
Harold Clements
 
The listview has, by default, the ability to 'Multi-select', so the relevant item you're interested in is held in a collection.

If you have disabled multiselect, you should be able to retrieve the index using:

myListView.SelectedIndices(0)

I'd personally put a check to ensure that at least ONE item is selected before issuing that code, or you will end up with an IndexOutOfBoundsException.. Something like:

If myListView.SelectedIndices.Count >0 then
myIndex = myListView.SelectedIndices(0)
' <your code here >
End If


Just like that!
 
MyListView.SelectedItems(0).Text to get info from first column

and

MyListView.SelectedItems(0).SubItems(ThecolumnNumber).Text to get info from other columns
 
Back
Top