Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

Counting Characters in TMemo

Hello
Is there any way I can quickly and simply count the number of characters in a TMemo field?
0
wireframe
Asked:
wireframe
1 Solution
 
esoftbgCommented:
function CharCount(Memo: TMemo): Integer;
var
  I:     Integer;
  S:     WideString;
begin
  I := 0;
  try
    S := Memo.Text;
    I := Length(S);
  finally
    Result := I;
  end;
end;
0
 
SimesACommented:
Simply Length(Memo1.Text) or Length(Memo1.Lines.Text) will do it, although the count will also include carriage-return and line-feed characters. You can use the following if you don't want to include those characters:
   Length(Memo1.Text) - 2 * Memo1.Lines.Count
0
 
DaFoxCommented:
Length(Memo1.Text) is faster than Length(Memo1.Lines.Text)!
However, I would use SendMessage(Memo1.Handle, WM_GETTEXTLENGTH, 0, 0) ;-)
So, using WM_GETTEXTLENGTH - (EM_GETLINECOUNT * 2) should be the fastest way, I guess.

Markus
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sfockCommented:
DaFox
is that any kind of joke i am unable to understand?
0
 
DaFoxCommented:
sfock, my intent was to help wireframe not to crack jokes!
If you would be more precise, I perhaps could help you, too.

Markus
0
 
nildoCommented:
length(memo1.text)
0
 
DaFoxCommented:
ShowMessage(IntToStr(SendMessage(Memo1.Handle, WM_GETTEXTLENGTH, 0, 0) - 2 * (SendMessage(Memo1.Handle, EM_GETLINECOUNT, 0, 0) - 1)));
0
 
wireframeAuthor Commented:
Thankyou all! esoftbg, Thankyou for your answer, it was just as good as Nildo's, but his was simply more concise.
Lots of help, thanks guys :)
0
 
sfockCommented:
DaFox
I am really delighted, that you are willing to help me too;-)
I will have to explain, that when i was reading your post i felt unable to beleve that you meant it honest,
so i migth get a little bit more precise then:

1.
>Length(Memo1.Text) is faster than Lengt (Memo1.Lines.Text)!
That is true, but Memo1.Lines.Text dirctly calls Memo1.Text so it does save actually 1 stack Frame after the complier is ready with his work it will leave about 1-3 cpu cycles more. On a standard 1GHz Mashine this is a maximum of 3/50000000000 seconds or 0,0000000006 milli seconds. Wich had let me think:"one can not mean this as a tip"
2. The solution with a sendMessage will work too (and mabe it will save another 10 CPU cycles), but
a. Making extensive use of SendMessage makes debugging a horror
b. it makes the code less readable and with this harder to maintain
c. SendMessage is a api command, the VCL was created to create an abstraction layer between the API and the programmer to make programming easier. So if i want to do core api programming i'd better take a look at C++ not Delphi
d. like nearly all api command's you have no chance to port the code to kylix
As a conclusion : nothing against using SendMessage if nessesary (what was a bad fact in Delphi1 in many cases) but never use them without a really good reason unless the costs are so high.
3. What you meant with "WM_GETTEXTLENGTH - (EM_GETLINECOUNT * 2)" will return wrong results when the memo has word wrap true.

That was precisely what wrapped trough my head and that was the reason i asked that stupid. But anyhow let me point out, that your solutions do work as well (under Windows) like the suggestions of the others
0
 
DaFoxCommented:
sfock, I entirely agree. Sorry for the misunderstanding.

Markus
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now