本文介绍了TMEMO中的多行添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个按钮(Button1,Button2)和1个备注(Memo1)的Delphi XE2项目。

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

我的要求是,在Button1上单击一些文本,然后将它们替换到第一行(Line1)中的Memo1。如果我再次单击Button1,则会在换行(Line2)中写入一些新文本。

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

如果单击Button2,则另一个新文本将添加到Memo1中(在最后一行之后将创建新行)。因此,我编写了以下代码:

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;

但问题是,在Button1FirstClick, Line2上仅显示一行带有 Line1的文本在Button1SecondClick上为,在Button2Click上为 Line4。请帮助我。

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.

推荐答案

要将更多文本添加到备注控件中,请调用追加 Add ,例如:

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

删除其中一行...

Memo1.Lines.Delete(2);

这篇关于TMEMO中的多行添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:09