Link to home
Start Free TrialLog in
Avatar of jdelozier
jdelozier

asked on

Total number of lines in RichTextBox

How can I determine the total number of lines in a richTextBox before the verticle scrollbar appears based on Font and Font Size/attributes?
Also, total number of lines before the horizontal scrollbar appears?

I prefer Managed C++ .net code but C# is fine too.
Avatar of rajeev_devin
rajeev_devin

Avatar of jdelozier

ASKER

I've looked at the first link above before and the second I've seen also.  I'll look more into these and see if I can find what I need.  By both of your comments, if I can get the line numbers I can tell what line the textbox ends on.  
Found a way to do it using Font Measurement.

                   // Create dummy string for measurement
                   String^ text = "TEST STRING";
                   System::Drawing::Font^ textFont = tbReader->Font;
                  
                   //Specify a fixed width, but let the height be "unlimited"
                   SizeF^ layoutSize = gcnew SizeF((float)tbReader->Width,
                         (float)5000.0F);
                   Graphics^ g = Graphics::FromHwnd(tbReader->Handle);
                   SizeF^ stringSize = g->MeasureString(text, textFont, (SizeF)layoutSize);

                   // Divide RTB height by font height to determine how many it can hold.
                   int numLines = tbReader->Height / stringSize->Height;

                   // Return the number
                   return numLines;
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial