2

I have a form with 4 edit boxes and a memo and am trying to figure out how to implement multiple undo's redo's, so for example-

So if the user enters text in edits 3,1,2, memo then edit 4 I want to undo in the revrese order and also redo in the original order.

However I am not really sure as to how to track the order. Any help on this would be appreciated

I am using Delphi 7

mjn
  • 36,362
  • 28
  • 176
  • 378
colin
  • 2,983
  • 6
  • 41
  • 49
  • See [Command design pattern](http://www.tabdee.ltd.uk/papers/MoreDesignPatterns.html#_Toc12157331) and [Memento design pattern](http://www.tabdee.ltd.uk/papers/MoreDesignPatterns.html#_Toc12157332). – NGLN Jul 04 '12 at 06:51

3 Answers3

4

The easiest way is to capture any change to any of included control and write down the state (contents, selection, caret position) of all 4 controls together. Do this for every change and you have an undo stack. Now when you need to undo the operation, you restore all controls to their saved state.

This scheme can be quite resource-consuming and once implemented you would want to optimize it by saving only the difference or the last operation done on particular control. The problems are

(a) standard edit / memo won't give you information on what exactly has changed, so you would need to somehow calculate the change (eg. by comparing it with previously recorded state, but this means that you'll need to "rebuild" contents from the initial state and through all the changes, then compare current-1 state with current state just to find out the change,

(b) standard controls handle undo operation themselves and the user can revert one control thus breaking the order of your undo stack.

Maybe you can use some third-party edit / memo control that would be more tolerant to your needs and will let you control undo/redo operations. For example, I once created TElEdit control for ElPack (now LMD ElPack owned by LMD Innovative) and that control among other features had undo/redo stack and I know how TElEdit could assist you with slight modifications of its source code. You might want to check TElEdit or other similar component to see what they can offer you.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
  • The "reverse delta" technique would reduce the speed to undo text changes beginning with the last version – mjn Jul 02 '12 at 12:23
1

Use the Command pattern for this. I won't explain the entire concept as there is plenty out there already, but a good example can be found here, and there are lots of others if you search for "Command pattern undo redo"

Alan Clark
  • 2,017
  • 21
  • 28
0

use a TClientDataSet control as a mem storage and 4 tdbedit controls. See TClientDataSet revert, undolastchange method.

Zozito
  • 9
  • 1