Select index from listview

c#

Solution

Because ListView doesn't contain any `SelectedIndex`, instead there is a property of `SelectedIndices`.

var indices = lvRegAnimals.SelectedIndices;
//indices[0] you can use that to access the first selected index

ListView.SelectedIndices

When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView.

Problem

I'm having some problem to get the index of the selected row in a listview. I wonder why this code isn't working? I get a red line below the SelectedIndex ``` private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e) { int index = lvRegAnimals.SelectedIndex; string specialData = motelManager.GetInfoFromList(index); UppdateSpecialData(specialData); } ``` Help is preciated. Thanks! EDIT: For some strange reason I get two messages when I click on one of the lines in the listView!? First I get the previous number and then the number for the last clicked line. What could be wrong? ``` private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e) { int index = lvRegAnimals.FocusedItem.Index; MessageBox.Show(Convert.ToString(index)); } ``` It's working now when I added a check like this: ``` if(lvRegAnimals.SelectedIndices.Count > 0) ```

Original source