Recent

Author Topic: [SOLVED] Undoing text editing in TEdit, etc.  (Read 147 times)

egsuh

  • Full Member
  • ***
  • Posts: 239
[SOLVED] Undoing text editing in TEdit, etc.
« on: September 18, 2019, 11:48:52 am »
Is it possible to undo editings in TEdit, etc.?
For example, the original Edit1.Text = 'Edit1'.  And I tried to change some part of it, but I want to leave the EditBox just leaving the original 'Edit1' text, ignoring my own  changes?
« Last Edit: September 18, 2019, 12:13:33 pm by egsuh »

wp

  • Hero Member
  • *****
  • Posts: 6225
Re: Undoing text editing in TEdit, etc.
« Reply #1 on: September 18, 2019, 12:07:07 pm »
Press ESC to restore the original content. I think it is not ready-made, but you can easily add it by yourself:
  • Add a string variable to the form named FOldEdit1Text
  • Write a handler for Edit1's OnEnter event copying is Text to FOldEdit1Text.
  • Write a handler for Edit1's OnKeyDown event checking the pressed key for ESCAPE and replacing the Text by FOldEdit1Text.
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     procedure Edit1Enter(Sender: TObject);
  18.     procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  19.     procedure Edit2Enter(Sender: TObject);
  20.     procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  21.   private
  22.     FOldEdit1Text: String;
  23.     FOldEdit2Text: String;
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. uses
  37.   LCLType;
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.Edit1Enter(Sender: TObject);
  42. begin
  43.   FOldEdit1Text := Edit1.Text;
  44. end;
  45.  
  46. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  47.   );
  48. begin
  49.   if Key = VK_ESCAPE then begin
  50.     Edit1.Text := FOldEdit1Text;
  51.     Edit1.SelectAll;
  52.     Key := 0;
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.Edit2Enter(Sender: TObject);
  57. begin
  58.   FOldEdit2Text := Edit2.Text;
  59. end;
  60.  
  61. procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  62.   );
  63. begin
  64.   if Key = VK_ESCAPE then begin
  65.     Edit2.Text := FOldEdit2Text;
  66.     Edit2.SelectAll;
  67.     Key := 0;
  68.   end;
  69. end;
  70.  
  71. end.  
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 239
Re: Undoing text editing in TEdit, etc.
« Reply #2 on: September 18, 2019, 12:13:06 pm »
@wp
I see. The function is not built-in. Writing such codes is not big deal.