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

标签 delphi richedit

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

最佳答案

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

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

关于delphi - 如何将文本附加到 RichEdit 控件的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1986520/

相关文章:

windows - 在 Delphi 应用程序的图标上动态插入数字

c++ - Rich Edit Control 更改 DialogBox 返回值行为?

delphi - 如何将RTF字符串添加到RichEdit?

c++ - 在 RichEdit winapi 中隐藏插入符号

delphi - 在没有焦点的情况下滚动 RichEdit

delphi - 如何使用 TStringHelper 测试两个字符串的序数不区分大小写相等?

delphi - 如何在 Delphi 中调试从 DLL 调用的 DLL

delphi - 为什么包含 FastMM 会导致 EInvalidTypecast 错误?

delphi - 如何对组合框中的项目进行排序?

delphi - 加载文本时的中文符号