RichTextBox Newline Conversion?

☆樱花仙子☆ 提交于 2019-11-27 14:24:25

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
    */
}
  var rtb = new RichTextBox();
  string enl = "Cheese" + Environment.NewLine + "Whiz";
  rtb.Text = enl;

This is a side-effect of the way the Text property works. It is cached in Control.Text, the actual native Windows control doesn't get updated until it is created. Problem is, that never happened with your rtb. You didn't add it to a form so the native control did not get created. Typical lazy resource allocation pattern in .NET. Consequently, you are reading the cached value, not the value from the control.

To see this, modify the code to force the control to be created:

        var rtb = new RichTextBox();
        rtb.CreateControl();
        string enl = "Cheese" + Environment.NewLine + "Whiz";
        rtb.Text = enl;

And you'll see that \r\n now is translated to \n. Don't forget to Dispose() the control.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!