1

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

4

3 回答 3

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];
于 2009-12-31T18:42:03.603 回答
1

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

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

于 2009-12-31T18:37:25.313 回答
1

履行

with RichEdit1 do
begin
 Lines.Add(s);
 Perform( EM_SCROLL, SB_LINEDOWN, 0);
end;
于 2015-07-15T09:06:30.387 回答