0

What I am trying to do is make a button that takes the amount of chars in a memo and output it in a label

I apologise if this seems like a silly question but I am still learning about Delphi.

2 Answers2

5

The easiest way is to call the Memo's GetTextLen() method:

Returns the length of the control's text.

procedure TForm1.Button1Click(Sender: TObject);
var
  Len: Integer;
begin
  Len := Memo1.GetTextLen;
  Label1.Caption := IntToStr(Len);
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

There are several ways to get the length of text in a TMemo. The fastest is to ask Windows to return the length (It is Windows which implement a TMemo):

MemoLen := GetWindowTextLength(Memo1.Handle);

or

MemoLen := SendMessage(Memo1.Handle, WM_GETTEXTLENGTH, 0, 0);

Another way is to retrieve the text of the memo and query his length:

MemoLen := Length(Memo1.Text);
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • `Length(Memo1.Text)` will work, but it is the least efficient way to do it, since it has to allocate a new string in memory and copy the entire Memo contents into it, just to query its length. Not a good idea for large texts. Since the Memo knows how many characters it is holding at all times, the best option is to simply ask it what that value is. – Remy Lebeau Sep 27 '20 at 17:14