Total number of lines in RichTextBox

Posted on 2006-03-29
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.
Question by:jdelozier
    LVL 12

    Expert Comment

    LVL 15

    Expert Comment


    Author Comment

    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 Comment

    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;
    LVL 1

    Accepted Solution

    PAQed with points refunded (125)

    Community Support Moderator

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now