Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Line Addition in TMEMO

Tags:

delphi

I am having one Delphi XE2 Project with 2 Buttons (Button1, Button2) and 1 Memo (Memo1).

My requirement is that on Button1 Click Some Text will be witten to Memo1 in the First Line (Line1). If I click again on Button1 Some New Text will be written in a New Line (Line2).

If I click on Button2 the Another New Text will be appended in Memo1 (After Last Line a new Line will be created). So I have written the following code :

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Text :='Line1';
  Memo1.Lines.Text :='Line2';
end;
....
....
....
....
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.Text :='Line3';
  Memo1.Lines.Text :='Line4';
end;

But the problem is that only one line is showing with text as "Line1" on Button1FirstClick, "Line2" on Button1SecondClick and "Line4" on Button2Click. Please help me.

like image 617
Rubi Halder Avatar asked Apr 14 '13 19:04

Rubi Halder


2 Answers

To add more text to a memo control, call either Append or Add, like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add('Line1');
  Memo1.Lines.Add('Line2');
end;
....
....
....
....
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.Add('Line3');
  Memo1.Lines.Add('Line4');
end;

If you need to clear the contents...

Memo1.Lines.Clear;

And if you wish to replace a line (only if the index already exists):

Memo1.Lines[2]:= 'Replacement Text';

To delete one of the lines...

Memo1.Lines.Delete(2);
like image 126
Jerry Dodge Avatar answered Nov 07 '22 10:11

Jerry Dodge


TMemo.Lines is an object of type TStrings that has many string handling capabilities. Assigning the Text property rewrites all strings it contains.

You can add a single line after all other already present lines with:

Memo.Lines.Add('Text');

You can insert a line (at fourth position) with:

Memo.Lines.Insert(3, 'Text');

And you can add multiple lines:

Memo.Lines.Add('Line1'#13#10'Line2');
Memo.Lines.AddStrings(ListBox.Lines);
like image 20
NGLN Avatar answered Nov 07 '22 09:11

NGLN