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

  • SEARCH
  • Home
  • 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 6333607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:29:48+00:00 2026-05-24T18:29:48+00:00

I’m using a WinForms RichTextBox. It appears that when the RichTextBox is on a

  • 0

I’m using a WinForms RichTextBox. It appears that when the RichTextBox is on a form, \r\n gets converted to \n. Here’s a test:

I have two rich text boxes. One is richTextBox1, which is placed on the form:

  this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  this.SuspendLayout();
  // 
  // richTextBox1
  // 
  this.richTextBox1.Location = new System.Drawing.Point(37, 12);
  this.richTextBox1.Name = "richTextBox1";
  this.richTextBox1.Size = new System.Drawing.Size(100, 96);
  this.richTextBox1.TabIndex = 0;
  this.richTextBox1.Text = "";

The other is rtb, which I create on the spot. When I run this code (in the form’s load event):

  var rtb = new RichTextBox();
  string enl = "Cheese" + Environment.NewLine + "Whiz";
  rtb.Text = enl;
  string ncr = rtb.Text;
  MessageBox.Show(string.Format("{0}{1}{2}{3}---{4}{5}{6}{7}{8}{9}",
                                enl.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                ncr.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                Environment.NewLine,
                                (enl == ncr), Environment.NewLine,
                                enl.Contains(Environment.NewLine), Environment.NewLine,
                                ncr.Contains(Environment.NewLine)));
  /*
  Cheese\r\nWhiz
  Cheese\r\nWhiz
  ---
  True
  True
  True
  */
  richTextBox1.Text = enl;
  string ncr2 = richTextBox1.Text;
  MessageBox.Show(string.Format("{0}{1}{2}{3}---{4}{5}{6}{7}{8}{9}",
                                enl.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                ncr2.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                Environment.NewLine,
                                (enl == ncr2), Environment.NewLine,
                                enl.Contains(Environment.NewLine), Environment.NewLine,
                                ncr2.Contains(Environment.NewLine)));
  /*
  Cheese\r\nWhiz
  Cheese\nWhiz
  ---
  False
  True
  False
  */

The RichTextBox seems to be exhibiting some strange behavior. When I put text containing a \r\n into the box I just created, it stays the same (still contains the \r\n). However, when I put text containing an \r\n into the box on the form, the \r\n gets turned into \n.

My Questions: Is there a reason for this behavior (\r\n->\n)? Is this behavior documented somewhere? Can I count on it always being this way?

The case I posted here is my attempt at getting to the bottom of a problem I’ve been having with one of my forms in a different project, so I’d appreciate any input regarding this issue.

  • 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-24T18:29:51+00:00Added an answer on May 24, 2026 at 6:29 pm

    The RichTextBox.Text property is converting the assigned string into an rtf document according to the Rtf format codes specified in the RichTextBox.Rtf property. Since the ‘rtb’ instance is not being initialized the ‘Rtf’ format codes are empty, and it’s just echoing back your input. After ‘rtb’ is initialized it contains an empty rtf document (with format codes), which is the same (and correct) behavior as ‘richTextBox1’.

    Results:

    preinit  rtb.Rtf : ''
    postinit rtb.Rtf : '"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17\\par\r\n}\r\n"'
    richTextBox1.Rtf : '"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17\\par\r\n}\r\n"'
    richtextBox1.Rtf with cheese : '"{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\lang1033\\f0\\fs17 Cheese\\par\r\nWhiz\\par\r\n}\r\n"'
    

    Code:

    void Form1_Load(object sender, EventArgs e)
    {
        TestIt();
    }
    public void TestIt()
    {
        string enl = "Cheese" + Environment.NewLine + "Whiz";
    
        RichTextBox rtb = new RichTextBox();
        MessageBox.Show("preinit rtb.Rtf : '" + rtb.Rtf + "'");
        this.Controls.Add(rtb);
        MessageBox.Show("postinit rtb.Rtf : '" + rtb.Rtf + "'");
        MessageBox.Show("richTextBox1.Rtf : '" + richTextBox1.Rtf + "'");
    
        rtb.Text = enl;
        string ncr = rtb.Text;
        MessageBox.Show(string.Format("rtb: {0}{1}{2}{3}---{4}{5}{6}{7}{8}{9}",
                                      enl.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                      ncr.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                      Environment.NewLine,
                                      (enl == ncr), Environment.NewLine,
                                      enl.Contains(Environment.NewLine), Environment.NewLine,
                                      ncr.Contains(Environment.NewLine)));
        /*
        Cheese\r\nWhiz
        Cheese\nWhiz
        ---
        False
        True
        False
        */
        richTextBox1.Text = enl;
        MessageBox.Show("richTextBox1.Rtf with cheese : '" + richTextBox1.Rtf + "'");
        string ncr2 = richTextBox1.Text;
        MessageBox.Show(string.Format("richTextBox1: {0}{1}{2}{3}---{4}{5}{6}{7}{8}{9}",
                                      enl.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                      ncr2.Replace("\n", "\\n").Replace("\r", "\\r"), Environment.NewLine,
                                      Environment.NewLine,
                                      (enl == ncr2), Environment.NewLine,
                                      enl.Contains(Environment.NewLine), Environment.NewLine,
                                      ncr2.Contains(Environment.NewLine)));
        /*
        Cheese\r\nWhiz
        Cheese\nWhiz
        ---
        False
        True
        False
        */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.