TMemo undo deletion lazarus / 31-LCL ---------------------------------------------------------------------- dve_hrnr · 2013-02-25 10:13 I am trying to configure a toolbar & main menu to go with a TMemo and am having problems with deleting text (or rather, undoing the deletion). I have tried using TMemo.ClearSelection and TMemo.SetSelText:='' but neither allow Undo, athough the delete option on the TMemo right-click menu works fine. Is there some other way of deleting test, or something else I should be doing to enable Undo? ---------------------------------------------------------------------- howardpc · 2013-02-25 11:11 TMemo's storage is based on a class called TTextStrings which AFAIK has no support for an undo feature. You would need to create your own descendant class from TCustomMemo and add support for this functionality yourself. Alternatively, you could take advantage of other people's work and use a TSynEdit (or a TSynMemo) component rather than a TMemo, which already provides this functionality. ---------------------------------------------------------------------- Avishai · 2013-02-25 12:32 Which Lazarus, FPC versions are you using?  Also which OS? ---------------------------------------------------------------------- dve_hrnr · 2013-02-26 09:03 The versions I am using are (from the Lazarus About window): Lazarus 1.0.4 FPC 2.6.0 x86_64-win64-win32/win64 (Windows 7 Pro) I am also intigued to know what links the TMemo edit options to my buttons/menu (ie. how Paste automatically becomes enabled after a Cut, etc). Is this because I am using an action list and the pre-defined edit actions? ---------------------------------------------------------------------- Avishai · 2013-02-26 09:28 Instead of ClearSelection... you can try Memo1.CutToClipboard; and for 'Undo' use Memo1.PasteFromClipboard; But this will only give you one level of 'undo' and it won't be very sophisticated.  Two 'undo's would paste it twice unless you clear then ClipBoard. I have not tried it but maybe you could simulate the keystrokes for CTRL+X and CTRL+Z? ---------------------------------------------------------------------- dve_hrnr · 2013-02-26 13:24 Thanks for the suggestion - Undo actually works for CutToClipboard, so no need use PasteFromClipboard. I see from the bugtracker that undo for cut/copy/paste in a TMemo was reported & fixed as issue 0016497 - there was no mention of delete as part of this, so it probably doesn't work in the same way. Guess I'll have to look at work-arounds (or just remove Delete from the menu!!) ---------------------------------------------------------------------- Avishai · 2013-02-26 14:53 It looks like it won't take much effort to add "Undo" to TMemo. Just as a test I added: Code: [Select]TCustomMemo = class(TCustomEdit) ... public    procedure Undo; end; and: Code: [Select]procedure TCustomMemo.Undo; begin   inherited; end; Edit:  You could make a feature request on Bugtracker if you want.  It looks to me like it should be a normal part of TMemo. ---------------------------------------------------------------------- howardpc · 2013-02-26 17:46 I think it would be better to name such a class TUndoMemo or somesuch, and descend from TCustomMemo (the LCL class, not the one Avishai invented). That would give it wordwrap and other memo features that TCustomEdit lacks. Also, the Undo method perhaps should provide for those widgetsets that do not offer undo functionality, something like Code: [Select]procedure TUndoMemo.Undo; begin  if CanUndo then inherited Undo    else ShowMessage('Undo functionality is not supported');    // or you could raise an exception if CanUndo is False end; ---------------------------------------------------------------------- Avishai · 2013-02-26 18:42 Actually it looks like both TMemo and TEdit already have "Undo". Memo1.Undo and Edit1.Undo work for me. ---------------------------------------------------------------------- howardpc · 2013-02-26 20:05 How shortsighted of us! Ah well, one less wheel to reinvent. ---------------------------------------------------------------------- Avishai · 2013-02-26 20:07 What's that they say about forests and trees?