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


Total number of lines in RichTextBox

Medium Priority
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.
Watch Question


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

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.


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.