Cancelling the ListView SelectedIndexChanged event

G

Guest

I have a ListView that populates some fields whenever the user selects an item. However, if they change the data in the fields and then select a new item without saving, I want to display a message box that warns that their changes will be lost, and asks them to confirm whether to continue. Sounds simple

Unfortunately, the ListView's SelectedIndexChanged event cannot be cancelled, and it's actually called for each change in the selection. So, for example, if item 1 is selected and the user then selects item 2, the event is called twice: once to remove the selection of item 1, and then to add the selection of item 2. If you go from having one item selected to having five (for example), the event fires to remove your original selection, and then again for each of your new selections - six hits in all

Obviously, this makes it a pain to determine when the event should be cancelled and reset back to the original. I obviously don't want my message box appearing six times....

Does anyone have a workaround for this

Thanks..
 
C

Cor Ligthert

Hi Invisible

The selectionIndexChange event is the default however it is not said that
you should use it.
There is also the click event, the columnclick, mouseup, why not try one of
those.

I hope this helps?

Cor
 
C

Cor Ligthert

Hi Invisible,

I forgot, for this kind of questions are also newsgroups as
microsoft.public.dotnet.languages.vb and csharp (also for C++ and J#)

There are also special control newsgroups, however to that you can add as
crosspost, but they are in my opinion as dead as a newsgroup dead can be.

Cor
 
G

Guest

Thanks for the help, Cor. Unfortunately, those events don't trap movements using the arrow keys, so I'd need to repeat everything in the KeyUp event too. It's worth considering, though

I'll try posting on that other group you mentioned; see if anyone there has any other idea

Duncan
 
S

Shanti

Hi Invisible,

You can flag when data is changed. Display the message in
SelectedIndexChanged event only if this flag is true. After returning from
the message box, if you are not cancelling the event, set this flag to
false. Thus for subsequent firings of this event since this flag is false,
you will not be displaying the message box.

ThanQ...

InvisibleDuncan said:
I have a ListView that populates some fields whenever the user selects an
item. However, if they change the data in the fields and then select a new
item without saving, I want to display a message box that warns that their
changes will be lost, and asks them to confirm whether to continue. Sounds
simple.
Unfortunately, the ListView's SelectedIndexChanged event cannot be
cancelled, and it's actually called for each change in the selection. So,
for example, if item 1 is selected and the user then selects item 2, the
event is called twice: once to remove the selection of item 1, and then to
add the selection of item 2. If you go from having one item selected to
having five (for example), the event fires to remove your original
selection, and then again for each of your new selections - six hits in all.
Obviously, this makes it a pain to determine when the event should be
cancelled and reset back to the original. I obviously don't want my message
box appearing six times....
 

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

Top