Results 1 to 6 of 6

Thread: Cancelling the ListView SelectedIndexChanged event

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Cancelling the ListView SelectedIndexChanged event

    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 bugger 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...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    I have ran into similar circumstances. Try using the onleave to prompt for saving instead of the onchange event. That way you only have to prompt them on their final selection. You can always have a clause to call the listbox.focus method to return focus to the control if the person doesn't want to save.
    SCUZ

  3. #3

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    Thanks, Scuz, but unfortunately that won't solve the problem.

    If the user has made any changes to any of the 20 or so fields, I don't want to prompt them to save after each change to each field. They should be prompted either when they close the form, or when they go to select another item from the ListView. If they select a new item, it should prompt them to go back and save - and therefore cancel the ListView_SelectedIndexChanged event, going back to the original selected item (assuming they don't want to lose their changes).
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  4. #4
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: Cancelling the ListView SelectedIndexChanged event

    This might be an old topic, but was never 'resolved' and I have exactly the same problem..

    any ideas?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cancelling the ListView SelectedIndexChanged event

    You can't cancel an event that is notifying you that something has happened. You can only cancel an event that is notifying you that something is going to happen. If you only want to display a message when an item is deselected then you have to detect when the number of selected items is reduced. It's too late to stop it at that point though.

    The solution is to not use a ListView. Back in 2004, the alternative was a DataGrid, which did have its issues. Since .NET 2.0 though, we have had the DataGridView. That's the control you should be using.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: Cancelling the ListView SelectedIndexChanged event

    K, thanx..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width