有没有更好的方法向 TMemo 添加一些字符?

发布于 2024-09-16 08:14:41 字数 227 浏览 2 评论 0原文

我正在使用 TMemo 保存从串行端口接收到的字符以供查看。当他们到达时,我正在做:

Memo1.Text := Memo1.Text + sReceivedChars;

这工作正常,但我认为它效率相当低,必须在连接我的几个字符然后将其写回之前获取现有文本。我真的很想要一个“SendChars()”函数或类似的函数。有没有更好的方法来简单地在现有文本的末尾添加一些字符?

I am using a TMemo to hold received characters from a serial port for viewing. As they arrive I am doing:

Memo1.Text := Memo1.Text + sReceivedChars;

This works fine but I presume it is rather inefficient, having to get the existing text before concatenating my few characters and then writing it back. I would really like a 'SendChars()' function or something similar. Is there a better way of simply adding a few characters on the end of the existing text?

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

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

发布评论

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

评论(2

|煩躁 2024-09-23 08:14:41

我不知道你是否认为值得,但你可以这样做:

procedure TForm1.Button1Click(Sender: TObject);
var
  index: Integer;
  NewText: string;
begin
  NewText := 'Append This';
  index := GetWindowTextLength (Memo1.Handle);
  SendMessage(Memo1.Handle, EM_SETSEL, index, index);
  SendMessage (Memo1.Handle, EM_REPLACESEL, 0, Integer(@NewText[1]));
end;

I don't know if you think it's worthwhile, but you can do something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  index: Integer;
  NewText: string;
begin
  NewText := 'Append This';
  index := GetWindowTextLength (Memo1.Handle);
  SendMessage(Memo1.Handle, EM_SETSEL, index, index);
  SendMessage (Memo1.Handle, EM_REPLACESEL, 0, Integer(@NewText[1]));
end;
旧时浪漫 2024-09-23 08:14:41

如果您的文本位于多行中(TStrings 集合的字符串,它是 TMemo 的 Lines 属性的实际类型),那么您可以执行以下操作:

Memo1.Lines[Memo1.Lines.Count - 1] := Memo1.Lines[Memo1.Lines.Count - 1] + sReceivedChars;

因此,您将一些字符添加到备忘录的最后一行(字符串集合中的最后一个字符串),而不将整个文本放入单个字符串中。

If your text is in more than one line (strings of a the TStrings collection that is the actual type of the Lines property of the TMemo), then you could do this:

Memo1.Lines[Memo1.Lines.Count - 1] := Memo1.Lines[Memo1.Lines.Count - 1] + sReceivedChars;

So you add some chars to the last line (the last string in the string collection) of the memo, without taking the whole text into a single string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文