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

发布于 2024-08-16 03:47:36 字数 93 浏览 4 评论 0原文

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

How can I add text not to a new line but to the last existing line? Lines.Add and Lines.Append add text as a new line, and Lines.Insert needs a position that I don't know how to find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的那抹悲凉 2024-08-23 03:47:36

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

// 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];

You can use the last line itself, or the entire content:

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

Note that the first way is better, especially when the RichEdit contains a large amount of text. Reading and writing to RichEdit.Text can involve moving lots of text around in memory.

EDIT: After the OP's comment to my answer:

To format the text, save SelStart before adding, and then use SelLength and SelAttributes to apply formatting:

// 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];
爱格式化 2024-08-23 03:47:36

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

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

You can use "Strings" and "Count" properties.

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

无人问我粥可暖 2024-08-23 03:47:36

履行

with RichEdit1 do
begin
 Lines.Add(s);
 Perform( EM_SCROLL, SB_LINEDOWN, 0);
end;

Perform

with RichEdit1 do
begin
 Lines.Add(s);
 Perform( EM_SCROLL, SB_LINEDOWN, 0);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文