SelectedIndexChanged event with listview



I have a form with a listview to allow users to select an item. When they
select one I fill in controls below it for them to modify the values. They
can also just enter new values in the controls without selecting a listview
item in order to insert new records. After entering new data if they select
a row in the listview I prompt them to make sure they want to save the
changes. If they select 'Cancel' I want to not highlight the newly selected
row and leave the data in the detail controls as it was. Otherwise the new
row is selected and the listview item is used to populate the detail

The problem seems to be that when I clear the selected items for the
listview the selectedindexchanged event fires again. I get the yesnocancel
prompt twice. If I step through the code in the debugger I only get it once.
I tried using a bool to skip the handler when clearing the selection but it
seems like the highlighting of the selected row then sometimes happens after
the clearing so it ends up in the selected anyway. I tried playing with the
clicked event and others and the events don't seem to always happen in a
predictable order. Each thing I try seems to create new issues and it seems
this shouldn't be this difficult. I've done similar things with the datagrid
but would like to use listview.

Basically want to be able to prompt the user sometimes when they select a
new listview item and if they "cancel" I want to be able to clear the
selection or set the selected item to a prior selected item without prompting
them a second time.

Any help would be appreciated. Thanks in advance.


Bob Powell [MVP]

ListView fires the SelectedIndexChanged event whenever the list is

Essentially you have to selectively remove or add the event handler.

Windows Forms Tips and Tricks has an example of this.

Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks

Answer those GDI+ questions with the GDI+ FAQ

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


Thanks for the reply. I am already trying to remove the handler. When they
select cancel I am running this code:

this.lvInvoiceDetail.SelectedIndexChanged -= new


this.lvInvoiceDetail.SelectedIndexChanged += new

The event handler is still being fired somehow. I also checked to make sure
I wasn't adding multiple instances of the event handler to the event. When I
step through the code in the debugger I only get one instance of the
messagebox and when I just run it I get 2.




One other thing you could try is to create two Events in your ListView
sub-class, a BeforeSelected and an AfterSelected. You'd have to mess with the
Mouse Down/Up events and the Key Up/Down events to fire the proper events in
the proper order. It's a bit of work, but once done in the sub-class, you'll
never have to worry about it again.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question