?
Solved

Counting Characters in TMemo

Posted on 2003-03-05
10
Medium Priority
?
332 Views
Last Modified: 2010-04-04
Hello
Is there any way I can quickly and simply count the number of characters in a TMemo field?
0
Comment
Question by:wireframe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 8071388
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
 
LVL 1

Expert Comment

by:SimesA
ID: 8071476
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
 
LVL 6

Expert Comment

by:DaFox
ID: 8071501
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:sfock
ID: 8072841
DaFox
is that any kind of joke i am unable to understand?
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8073249
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
 
LVL 1

Accepted Solution

by:
nildo earned 200 total points
ID: 8074177
length(memo1.text)
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8074247
ShowMessage(IntToStr(SendMessage(Memo1.Handle, WM_GETTEXTLENGTH, 0, 0) - 2 * (SendMessage(Memo1.Handle, EM_GETLINECOUNT, 0, 0) - 1)));
0
 

Author Comment

by:wireframe
ID: 8074874
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
 
LVL 3

Expert Comment

by:sfock
ID: 8083408
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
 
LVL 6

Expert Comment

by:DaFox
ID: 8083882
sfock, I entirely agree. Sorry for the misunderstanding.

Markus
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question