2007. január 27., szombat

How to restrict the number of lines in a TMemo


Problem/Question/Abstract:

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).

TLimitedMemo = class(TCustomMemo)
private
  fChanging: Boolean;
protected
  procedure Change; override;
public
  constructor Create(AOwner: TComponent); override;
end;

procedure TLimitedMemo.Change;
var
  i: Integer;
begin
  if fChanging then
    Exit;
  inherited;
  with Lines do
  try
    BeginUpdate;
    if Count > 5 then
    begin
      fChanging := True;
      for i := 0 to Count - 6 do
        Delete(0);
      fChanging := False;
    end;
  finally
    EndUpdate;
  end;
end;

constructor TLimitedMemo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fChanging := False;
end;

In this case, this memo is allowing 5 lines(you can change it at your will).

Nincsenek megjegyzések:

Megjegyzés küldése