Compute number of lines typed in a rich text box control

John 511 Reputation points
2023-09-20T12:06:39.8033333+00:00

I would like to know more about computing the number of lines in a rich text box control.

From there, I would also like to include it in a for loop structure.

Also, what is the length property of the lines object of the rich text box control do?

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. KOZ6.0 6,735 Reputation points
    2023-09-21T03:09:15.1566667+00:00

    To find out the number of lines displayed in the RichTextBox, run the GetLineFromCharIndex method.

    private void Form1_Load(object sender, EventArgs e) {
        richTextBox1.Text = "Hello, World! \r\n Hello, World!Hello, World!Hello, World!Hello, World!";
    }
    
    private void RichTextBox1_TextChanged(object sender, EventArgs e) {
        RichTextBox rbox = (RichTextBox)sender;
        int len = rbox.TextLength;
        int lines;
        if (len > 0) {
            lines = rbox.GetLineFromCharIndex(len - 1) + 1;
        } else {
            lines = 0;
        }
        label1.Text = $"lines:{lines}";
    }
    

    enter image description here

    see also:

    GetCharIndexFromPosition
    GetPositionFromCharIndex

    There are many messages to provide layout information.

    「Edit Control Messages」

    https://learn.microsoft.com/en-us/windows/win32/controls/bumper-edit-control-reference-messages

    EM_GETLINECOUNT, EM_LINEFROMCHAR, ...etc

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-09-20T13:16:44.3566667+00:00

    Hi, @John

    Lines is Array type string[]. And Length properties: Gets the total number of elements in all the dimensions of the Array. Length represents the number of lines (end with \r\n is one line).

    Eg.
    richTextBox1.Text = "Hello, World! \r\n Hello, World!Hello, World!Hello, World!Hello, World!";

    Enabled word wrap, but the length is 2.

    User's image

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.