TEdit and TMemo's OnChange events have different responses lazarus / 31-LCL ---------------------------------------------------------------------- tomitomy · 2017-11-10 05:56 Hi, I'm in trouble, I implemented Undo Redo for TMemo, and I want to use it also in TEdit, but the result is wrong, then I found TEdit and TMemo's OnChange events have different responses, can they be unified? Test Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  Edit1.Text := 'ABCDEFG';  Memo1.Text := 'ABCDEFG';end; procedure TForm1.Memo1Change(Sender: TObject);begin  writeln('TMemo: ', Memo1.SelStart, '  ', Memo1.SelLength);end; procedure TForm1.Edit1Change(Sender: TObject);begin  writeln('TEdit: ', Edit1.SelStart, '  ', Edit1.SelLength);end; ---------------------------------------------------------------------- howardpc · 2017-11-10 11:17 Quote from: tomitomy on November 10, 2017, 06:56:28 am I found TEdit and TMemo's OnChange events have different responses, can they be unified? Lazarus "unifies" different widgetset implementations of the various native edit widgets, and it similarly "unifies" the various native memo widgets - but there is no guarantee that such "unified" edit and memo events are going to correspond exactly, since the timing of these events is completely widgetset-dependent. Coincidentally a Win32 memo widget may fire an OnChange event at the "same" moment that a GTK2 edit widget fires its OnChange event, but equally there may be no correspondence between the behaviour of these very different widgets. Better to find and use an event in each control that is more likely to be logically synchronised. Try OnKeyUp, which (at least on Linuxes) gives identical SelLength and SelStart between TEdit and TMemo. ---------------------------------------------------------------------- tomitomy · 2017-11-10 12:16 Thank you howardpc, I feel so tired of implementing Undo and Redo in this way, and I don't want to go on, just stop here. ---------------------------------------------------------------------- howardpc · 2017-11-10 15:35 TEdit does not need nearly such a sophisticated undo function as TMemo. Most uses of TEdit are of short duration, during which a fairly short string is normally edited. So an inefficient implementation is probably fine for those platforms where the widgetset does not implement an Undo. Something as simple as the following. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit uHistoryEdit; {$mode objfpc}{$H+} interface uses  sysutils, Classes, StdCtrls, LCLType, Controls; type   { THistoryEdit }   THistoryEdit = class(TEdit)  private    history: TStringArray;    procedure AddRecord(const aText: String);  protected    procedure KeyUp(var Key: Word; Shift: TShiftState); override;    procedure SetParent(NewParent: TWinControl); override;  public    procedure PerformUndo;  end; implementation { THistoryEdit } procedure THistoryEdit.AddRecord(const aText: String);begin  SetLength(history, Length(history)+1);  history[High(history)]:=aText;end; procedure THistoryEdit.KeyUp(var Key: Word; Shift: TShiftState);begin  inherited KeyUp(Key, Shift);  case Key of    VK_RETURN..VK_MENU,    VK_PRIOR..VK_INSERT: ;    else AddRecord(RealGetText);  end;end; procedure THistoryEdit.SetParent(NewParent: TWinControl);begin  inherited SetParent(NewParent);  if Assigned(NewParent) then    AddRecord(RealGetText);end; procedure THistoryEdit.PerformUndo;begin  if Length(history) > 1 then begin    SetLength(history, Length(history) - 1);    RealSetText(history[High(history)]);  end;end; end. ---------------------------------------------------------------------- tomitomy · 2017-11-11 02:58 You're right, TEdit just needs a simple history function, thank you howardpc.