1

What i'm trying to do is select an item in my listview, and it works! That is it works once, the first time a select an item it go's well, the second time a get an argument out of range exception on features[0].SubItems[1].Text; on the zero.

this is what i have:

private void listViewFeatures_SelectedIndexChanged(object sender, EventArgs e) 
{
    ListView.SelectedListViewItemCollection features = listViewFeatures.SelectedItems;
    string feature = features[0].SubItems[1].Text;
    BL_AddReport addReport = new BL_AddReport(this.databaseConnectionString);

    Dictionary<string, bool> pictures = addReport.GetpicturesFromFeature(feature);

    foreach (KeyValuePair<string, bool> pic in pictures) 
    {
        if (pic.Value) {
            pictureBoxCar.Image = Image.FromFile(pic.Key);
        }
        else 
        {
            pictureBoxEquip.Image = Image.FromFile(pic.Key);
        }
    }
}     

Does anyone know what the problem is?

Alex Mendez
  • 5,120
  • 1
  • 25
  • 23
jorne
  • 894
  • 2
  • 11
  • 23

1 Answers1

2

I'm betting you'd get this exception if you clicked off of the listview as well.

Remember that this event is for selection changes.. which may mean that something was selected and now nothing is. In fact, according to this an event is fired once for every thing that is selected. Take a look at that link for more information and designs around this problem if that is the case for you.

Otherwise just check to make sure that your "features" variable has anything inside of it before indexing into it

Community
  • 1
  • 1
BoredAndroidDeveloper
  • 1,251
  • 1
  • 11
  • 26