如何将文本追加到RichEdit控件的最后一行?

时间:2009-12-31 18:23:17

标签: delphi richedit

如何将文字添加到新行但不添加到最后一行? Lines.Add和Lines.Append将文本添加为​​新行,而Lines.Insert需要一个我不知道如何查找的位置。

2 个答案:

答案 0 :(得分:3)

您可以使用最后一行本身或整个内容:

// RE = TRichEdit, Temp = string;
// Last line only
Temp := RE.Lines[RE.Lines.Count - 1];
Temp := Temp + ' plus some new text';
RE.Lines[RE.Lines.Count - 1] := Temp;

// The entire content
Temp := RE.Text;
Temp := Temp + ' plus some new text';
RE.Text := Temp;

请注意,第一种方法更好,特别是当RichEdit包含大量文本时。读取和写入RichEdit.Text可能涉及在内存中移动大量文本。

编辑:OP对我的回答发表评论后:

要格式化文本,请在添加之前保存SelStart,然后使用SelLength和SelAttributes应用格式:

// StarPos and Len are both Integers.
StartPos := Length(RE.Text);
Len := Length(YourNewTextToBeAdded);
// Do stuff here to add text
RE.SelStart := StartPos;
RE.SelLength := Len;
RE.SelAttributes.Style := RE.SelAttributes.Style + [fsBold];

答案 1 :(得分:1)

您可以使用“字符串”和“计数”属性。

RichEdit1.Lines.Strings [RichEdit1.Lines.Count-1]:= RichEdit1.Lines.Strings [RichEdit1.Lines.Count-1] + '文本';