I have a list view control that users can add items to but I need to be able to tell what item they have selected.
A:
The SelectedItems property returns a list of which of the list view items have been selected. If you only want them to be able to select a single item you can set the MultiSelect property to false.
TLiebe
2009-10-09 19:06:22
I come up with an error that says it cannot convert it into an integer, and I can't find a way to parse the value.
muckdog12
2009-10-09 19:08:38
Can you post some of your code? The SelectedItems property returns a collection of ListViewItem objects. If you want the index of the item as an integer look at the Index property of the individual ListViewItem. For example, lst.SelectedItems(0).Index will give you the index of the first selected item.
TLiebe
2009-10-09 19:12:29
Private Sub lbxLog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxLog.SelectedIndexChanged'intCurrent is an IntegerintCurrent = lbxLog.SelectedIndicesEnd Sub
muckdog12
2009-10-09 19:18:10
Sorry, it just bunched it all up
muckdog12
2009-10-09 19:18:44
A:
Use the SelectedIndices
property. The SelectedItems
property will return you the actual objects that are in the list (which you may have to do anyways).
EDIT: Sample (sorry, my VB may be rusty):
If myListView.SelectedIndices.Count > 0 Then
int selectedIndex = myListView.SelectedIndices[0];
' Do other stuff
End If
Jon Seigel
2009-10-09 19:09:19
It returns the same error and there still is no way to parse the value
muckdog12
2009-10-09 19:11:18
It's a collection of values, because the user could possibly select multiple items. If your ListView is in single-selection mode, use SelectedIndices[0]. But make sure to test SelectedIndices.Count to see if anything is selected.
Jon Seigel
2009-10-09 19:14:52
I am getting an error that says its an Invalid Argument. It is saying that the value of 0 is not valid for 'index'
muckdog12
2009-10-09 19:28:45
See my previous comment about testing SelectedIndices.Count -- if there is no selection, this collection is empty, and trying to get the value at index 0 will result in an error. I will put an example in my answer.
Jon Seigel
2009-10-09 19:31:21
Thanks for all your help, intcurrent value in the wrong block in the count check. Thanks!
muckdog12
2009-10-09 19:34:51