Question about SelectedItems in ListViews

  • Thread starter Thread starter Beringer
  • Start date Start date
B

Beringer

Hello,

I have a listview and have set the MultiSelect property to false.

I assumed that if I monitored the OnSelectedIndexChanged event that I could
obtain the currently selected item from the SelecteItems collection by
referencing the 0 index.

When I do this however, I get a runtime exception telling me I have exceded
the size of the collection. The debugger shows the collection to be empty.

So my question is, "Is the OnSelectedIndexChanged event created before or
after the SelectedItems collection is updated?"

I have found that if I use the ItemActivate event my code works but I don't
really want to use ItemActivate.

I have provided a snipet of code below.
private void OnSelectedIndexChange_lvSearchResults(object sender,
System.EventArgs e) {

ListView.SelectedListViewItemCollection items =
(ListView.SelectedListViewItemCollection)lv.SelectedItems;

ListViewItem selectedItem = (ListViewItem)items[0];

etc.

}

The exception is thrown on the second line.



Thank you in advance,

Eric
 
I have found that using SelectedItems property in ListView a bit tricky,
especially you want to get selected item(s) in SelectedIndexChanged event
handler.

I think what is happening is when you click an item in the listview
(MultiSelect sets to False), SelectedIndex first becomes -1 from the value
before clicking, and then it changes to the clicked item's index. That is,
SelectedIndexChanged event is fired twice (I just did a test to verify
this). Therefore, the SelectedItems first removes previous item, and becomes
empty, then the clciked item is added in.

So, if I want to get the selected item in SelectedItems collection in
SelectedIndexChanged event handler, I always do it this way:

private void LisView1_SelectedItemChanged(object sender...)
{
if ListView1.SelectedItems.Count==0) return;

//Now you know there is at least an item is the SelectedItems collection
ListViewItem myItem=ListView1.SelectedItems[0]

//Do thing with the selected item

}
 
Thank you that really helps.
Doesn't seem right but I understand what you said.

Eric

Norman Yuan said:
I have found that using SelectedItems property in ListView a bit tricky,
especially you want to get selected item(s) in SelectedIndexChanged event
handler.

I think what is happening is when you click an item in the listview
(MultiSelect sets to False), SelectedIndex first becomes -1 from the value
before clicking, and then it changes to the clicked item's index. That is,
SelectedIndexChanged event is fired twice (I just did a test to verify
this). Therefore, the SelectedItems first removes previous item, and becomes
empty, then the clciked item is added in.

So, if I want to get the selected item in SelectedItems collection in
SelectedIndexChanged event handler, I always do it this way:

private void LisView1_SelectedItemChanged(object sender...)
{
if ListView1.SelectedItems.Count==0) return;

//Now you know there is at least an item is the SelectedItems collection
ListViewItem myItem=ListView1.SelectedItems[0]

//Do thing with the selected item

}

Beringer said:
Hello,

I have a listview and have set the MultiSelect property to false.

I assumed that if I monitored the OnSelectedIndexChanged event that I could
obtain the currently selected item from the SelecteItems collection by
referencing the 0 index.

When I do this however, I get a runtime exception telling me I have exceded
the size of the collection. The debugger shows the collection to be empty.

So my question is, "Is the OnSelectedIndexChanged event created before or
after the SelectedItems collection is updated?"

I have found that if I use the ItemActivate event my code works but I don't
really want to use ItemActivate.

I have provided a snipet of code below.
private void OnSelectedIndexChange_lvSearchResults(object sender,
System.EventArgs e) {

ListView.SelectedListViewItemCollection items =
(ListView.SelectedListViewItemCollection)lv.SelectedItems;

ListViewItem selectedItem = (ListViewItem)items[0];

etc.

}

The exception is thrown on the second line.



Thank you in advance,

Eric
 
Back
Top