Author: Jonas Bilinkevicius
How can I get the following to happen with a memo: I would like to make it start
purging lines from the top when a line is added at the bottom after I have 1024
lines.
Answer:
You can do the following (I am making the example for TCustomMemo to make this more
general).
1 2 TLimitedMemo = class(TCustomMemo)
3 private4 fChanging: Boolean;
5 protected6 procedure Change; override;
7 public8 constructor Create(AOwner: TComponent); override;
9 end;
10 11 procedure TLimitedMemo.Change;
12 var13 i: Integer;
14 begin15 if fChanging then16 Exit;
17 inherited;
18 with Lines do19 try20 BeginUpdate;
21 if Count > 5 then22 begin23 fChanging := True;
24 for i := 0 to Count - 6 do25 Delete(0);
26 fChanging := False;
27 end;
28 finally29 EndUpdate;
30 end;
31 end;
32 33 constructor TLimitedMemo.Create(AOwner: TComponent);
34 begin35 inherited Create(AOwner);
36 fChanging := False;
37 end;
In this case, this memo is allowing 5 lines(you can change it at your will).