Undo not working right (and yes I have unique Ids)

Undo not working right (and yes I have unique Ids)

Old forum URL: forums.lhotka.net/forums/t/2731.aspx


ABecerra posted on Wednesday, April 18, 2007

Here is the basic scenario... I have a simple form which has a listbox, databound to a SortedBindingList<> of my objects. Each object is my own rule object. The listbox is used to display the rules and there are up/down arrows for the user to move the rules up and down the list (altering the priority order of the rules).

When the UI first displays, I output some debug info (this is before any edits are made, so I have not called BeginEdit at this point.

Rule priorities...Initial setup (NO call to BeginEdit yet)
Rule Id: 71, Priority: 1
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 3

Below, the user clicked rule 73, and moved it up. I call BeginEdit to start swapping the priority order...
Rule priorities...moved up 1
Rule Id: 71, Priority: 1
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 3

And then call begin edit to finish swapping the priority order.
Rule priorities...moved up 2
Rule Id: 71, Priority: 1
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 2

Here, the user moved rule 73 up again. I call BeginEdit to start swapping the priority order...
Rule priorities...moved up 1
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 2

And then call begin edit to finish swapping the priority order.
Rule priorities...moved up 2
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 1

But when the user clicks cancel, I try to cycle through to call CancelEdit the same number of times BeginEdit was called. But the priorities do not get undone properly. As you can see from the debug messages, CancelEdit is not at all doing what it should. What am I doing wrong?
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 3
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 1
Rule priorities...Cancel clicked, CancelEdit called
Rule Id: 71, Priority: 2
Rule Id: 72, Priority: 2
Rule Id: 73, Priority: 1

RockfordLhotka replied on Wednesday, April 18, 2007

Remember, Windows Forms data binding will call BeginEdit/CancelEdit/EndEdit on your behalf. This happens whether or not you call BeginEdit by hand. It is unavoidable.

So if you are also calling BeginEdit, etc. without understanding that data binding is calling the methods too - then you are in for a world of confusion and hurt.

You really might need to call BeginEdit yourself, on the collection. But if so, you must do it before setting up data binding. Otherwise data binding will have already called BeginEdit on the current row (whichever one that is) and confusion will result.

ABecerra replied on Thursday, April 19, 2007

Thank you for the response. I am in a bit of time crunch, so I opted to not use data binding and the sortedBindingList for this simple UI screen and manually add my items to the listbox and manually sort based on the user clicking up/down buttons. I will revisit when I have more time and will add an example here for other people.

Thanks,

Andres

Copyright (c) Marimer LLC