Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way of adding a few characters to a TMemo?

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?

like image 218
Brian Frost Avatar asked Dec 28 '22 08:12

Brian Frost


2 Answers

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;
like image 170
500 - Internal Server Error Avatar answered Feb 07 '23 10:02

500 - Internal Server Error


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.

like image 37
SalvadorGomez Avatar answered Feb 07 '23 11:02

SalvadorGomez