C#: bizzare multiple event firing for listview on SelectedIndexChanged -


if put listview component windows form , add bellow code it's selectedindexchanged event:

messagebox.show("fired!"); foreach (int selectedindex in listview1.selectedindices) {     listview1.items[selectedindex].selected = false;     listview1.items[selectedindex].focused = false; } 

the message box shown 4 times! why that?

note: use loop clear selected items in listview

you should not change selection in selectedindexchanged event. more generally, you should not change property inside of notification property has been changed.

if need change property in response notification, handle corresponding *changing event. rather being notification has changed (which comes after fact), notification change (which comes before fact). in selectedindexchanging event, have couple of different options alter course of events:

  1. you can set e.cancel property true, says. cancel event , prevent selected index changing.
  2. you can use e.newselectedindex property alter selection. set property index of item want selected.

and if want clear selected items in listview in response other event (e.g., click on "clear selection" button not part of listview, or similar context menu item), don't need loop @ all. clear control's selecteditems collection: mylistview.selecteditems.clear(). again, can't in response selectedindexchanged event, or you'll have same problem of triggering bunch of notifications.

honestly, though, code you've written here makes no sense. why want clear selected items when user tries select item? if don't want allow selection, disable control setting enabled property false.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -