Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit no. of lines shown in a TMemo - show latest lines only

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
0
0
GB
hi

I'm using D7 and have a TMemo which receives quite a lot of text. Is there a way of limiting the number of lines shown so it always displays, say the last 20 lines added. That is, I want the older lines removing automatically. I thought I could use the Capacity property but no.

I know I could write some code to manipulate the TStrings but I'm hoping there is a simpler way, a property perhaps...

Many thanks in advance
Lou
 
Memo1.Lines.Capacity is just a statement of how much to initially allocate in memory.

You got to manipulate the TStrings, but not hard.

Code:
const
  Memo1Limit = 10;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Memo1Count: integer;
    ButtonCount: integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1Count := 0;
  ButtonCount := 0;
  Memo1.Lines.Capacity := Memo1Limit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  inc(Memo1Count);
  inc(ButtonCount);
  Form1.Caption := IntToStr(ButtonCount);
  Memo1.Lines.Add(IntToStr(ButtonCount));
  if Memo1Count > Memo1Limit then
    Memo1.Lines.Delete(0);
end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
You don't even need to limit it's capacity, what you could do is something like this:

Code:
procedure TForm1.Memo1Change(Sender: TObject);
begin
  while Memo1.Lines.Count > 20 do
    Memo1.Lines.Delete(0);
end;

Removes the top lines lines (assuming you use Lines.Add to add new lines to the memo here) when new lines are added, removes multiple if multiple are added at once.

If you use Insert(0) (so they are added at the top line) then use Memo1.Lines.Delete(Memo1.Lines.Count - 1); so the lines are removed from the bottom. Then you can just fix the height to whatever height you want.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top