views:

2309

answers:

3

I have a listbox(detailed view).My question is how do I get the index of the selected item?

Eventually,how to get the text at the index,but that should be easy.

+2  A: 

The selected index is in the SelectedIndex property.

The selected text is in the Text property.

Adam Robinson
There's no such thing listview1.SelectedIndex.
John
+3  A: 

ListBox.SelectedItem Property:

Gets or sets the currently selected item in the ListBox.

Or, naturally, ListBox.SelectedItems Propery:

Gets a collection containing the currently selected items in the ListBox.

... Remarks

For a multiple-selection ListBox, this property returns a collection containing all items that are selected in the ListBox. For a single-selection ListBox, this property returns a collection containing a single element containing the only selected item in the ListBox. For more information about how to manipulate the items of the collection, see ListBox..::.SelectedObjectCollection.

The ListBox class provides a number of ways to reference selected items. Instead of using the SelectedItems property to obtain the currently selected item in a single-selection ListBox, you can use the SelectedItem property. If you want to obtain the index position of an item that is currently selected in the ListBox, instead of the item itself, use the SelectedIndex property. In addition, you can use the SelectedIndices property if you want to obtain the index positions of all selected items in a multiple-selection ListBox.

gimel
I'm with .NET 3.5.I don't have it,I have Listbox.SelectedItems
John
Listbox.SelectedItems.ToString() returns "System.Windows.Forms.ListView+SelectedListViewItemCollection"
John
Try 1st element in collection, Listbox.SelectedItems[0].ToString()
gimel
A: 

I think you mean ListView (not ListBox) so use SelectedItems and SelectedIndices properties.

M. Jahedbozorgan