Rubi Halder
Rubi Halder

Reputation: 593

Multiple Line Addition in TMEMO

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.

Upvotes: 5

Views: 40014

Answers (3)

James Chong
James Chong

Reputation: 71

in Delphi has Memo1.Lines.Text

but in C builder has Memo1.Text or Memo1->Text

to let multiple lines into TMemo you can assigned it as memo1->text = tstringlist->text;

as tstringlist you can use tsringlist->CommaText="line1,line2,line3,line4"; // , as new line

then memo1->text = tstringlist->text;

or memo1->lines->add(tstringlist->text); //inserting after memo1 where the last line stop

or memo1->lines->add("Line 1, \x0d\x0a line 2, \x0d\x0a line 3");

or memo1->lines->add("Line 1, \r\n aline 2, \r\n aline 3");

else using memo1->lines->add(""); //for each new blank line

good luck

Upvotes: 1

NGLN
NGLN

Reputation: 43659

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

Upvotes: 7

Jerry Dodge
Jerry Dodge

Reputation: 27276

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

Upvotes: 12

Related Questions