3D-kreativ
3D-kreativ

Reputation: 9319

Select index from listview

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)

Upvotes: 2

Views: 34949

Answers (5)

GravityBug
GravityBug

Reputation: 1

The ListView is a darn hassle to work with sometimes. A simple solution i've used is a for loop that checks for the selected Item.

I've put my solution in the "When index change trigger" within the ListView. Example:

int sel_item = 0; //an int to store the selected item index.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
      for (int i = 0; i < listView1.Items.Count; i++)
      {
             if (listView1.Items[i].Selected == true)
             {
              sel_item = i;
             }
      }
}

This would ofcourse only work correctly with the "Multiselection" option set as false.

Upvotes: 0

Roger Deep
Roger Deep

Reputation: 172

There is another thread like this one, but here it goes again.

It can return NULL. Also the SelectedIndexChanged event can be FIRED TWICE. And the first time, there nothing selected yet.

So the only safe way to find it is like this:

    private void lv1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lv1.FocusedItem == null) return;
        int p = lv1.FocusedItem.Index;

... now int p has the correct value...

Upvotes: 0

jaleel
jaleel

Reputation: 391

Try :

listView1.FocusedItem.Index

This give you the index of the selected row.

Upvotes: 0

user1500065
user1500065

Reputation:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Acquire SelectedItems reference.
        var selectedItems = listView1.SelectedItems;
        if (selectedItems.Count > 0)
        {
        // Display text of first item selected.
        this.Text = selectedItems[0].Text;
        }
        else
        {
        // Display default string.
        this.Text = "Empty";
        }
    }

Upvotes: 0

Habib
Habib

Reputation: 223422

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.

Upvotes: 12

Related Questions