We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Total number of lines in RichTextBox

Medium Priority
278 Views
Last Modified: 2013-12-03
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.
Comment
Watch Question

Author

Commented:
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.  

Author

Commented:
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;
Commented:
PAQed with points refunded (125)

DarthMod
Community Support Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.