Is there something else going on in your example affecting the listView1
item? I've mocked up a small clone of your question and I can't seem to replicate the error:
I'm using your code for the event handler, and I'm populating the listView1
thusly:
listView1.Items.Add("Profesor 1");
listView1.Items.Add("Profesor 2");
It sounds like you're causing the listView1_SelectedIndexChanged
handler to be fired in the background.
As a slight aside, your array lists aren't going to change, you probably don't want to keep your ArrayList creation in your event handler and keep recreating them each time (they don't seem to have any dynamic information in). particularly as that handler will often be called twice once, for the deselection, and again for a selection.
On that last point, it's also worth having a look at the ItemSelectionChanged
event, which could be used to help your initial problem, as it comes with ListViewItemSelectionChangedEventArgs
which contain the property IsSelected
, which you could use to verify that you have an object selected.
e.g:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
// Your code here.
// e.Item...
}
}