Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3230606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:56:48+00:00 2026-05-17T16:56:48+00:00

I have a RichTextBox control on my form. The control is setup in such

  • 0

I have a RichTextBox control on my form. The control is setup in such a way that it will wrap to the next line after 32 lines of text are input. The problem I’m having is I want to be able to retreive an array of strings representing the lines in my control. I know there is a Lines property attached to the RichTextBox, but I am experiencing 2 issues with it:

1) I ONLY want an array of strings showing the lines that are visible on the screen only. Right now the Lines array returns every single line in the RichTextBox. I only want the lines visible on the screen returned.

2) The Lines property is not giving me a true representation of my lines. It counts a “line” as a line of text ended by a carriage return or \n. So in other words, if I type 64 characters and none of them are a carriage return, then it should return 2 lines (because there are 32 characters per line). Instead, it doesn’t return any lines until I hit enter. Even then, it only returns 1 line, not 2. Its acting more like a Paragraph property, if there was such a thing.

Anyone know a way around these 2 issues?I am using C# btw

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:56:49+00:00Added an answer on May 17, 2026 at 4:56 pm

    You have to do a few tricks to achieve this which have to do with querying for the position of the actual lines according to the character index. The following program shows one way of doing this. You might have to harden it a bit, but it should get you started:

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();                        
        }        
    
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (richTextBox1.Text == "")
                return; 
    
            // Lines corresponding to the first and last characters:
            int firstLine = richTextBox1.GetLineFromCharIndex(0); 
            int lastLine = richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length);
    
            // Get array of lines:
            List<string> lines = new List<string>();
            for (int i = firstLine; i <= lastLine; i++)
            {
                int firstIndexFromLine = richTextBox1.GetFirstCharIndexFromLine(i);
                int firstIndexFromNextLine = richTextBox1.GetFirstCharIndexFromLine(i + 1);
    
                if (firstIndexFromNextLine == -1)
                {
                    // Get character index of last character in this line:
                    Point pt = new Point(richTextBox1.ClientRectangle.Width, richTextBox1.GetPositionFromCharIndex(firstIndexFromLine).Y);
                    firstIndexFromNextLine = richTextBox1.GetCharIndexFromPosition(pt);
                    firstIndexFromNextLine += 1;
                }
    
                lines.Add(richTextBox1.Text.Substring(firstIndexFromLine, firstIndexFromNextLine - firstIndexFromLine));
            }
    
            // Print to richTextBox2 while debugging:
            richTextBox2.Text = "";
            foreach (string line in lines)
            {
                richTextBox2.AppendText(">> " + line + Environment.NewLine);
            }                        
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a RichTextBox control on a form with some text, some of which
I have a form with a rich text box control. Is there a way
I have a problem with .Net's RichTextBox control. It seems that it doesn't support
I have a WinForms application for editing text in a large RichTextBox control. Can
I have a method that is meant to display output on a RichTextBox in
I have extended the RichTextBox control to implement much of the missing functionality provided
My issue is that I've created an extended RichTextBox control that uses native API
I have a Windows Form with a RichTextBox on it. The content of the
I have details view control in my asp.net web form, which on of its
I have a RichTextBox on my form, and I want to use the default

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.