RichTextBox换行符转换?

我正在使用WinForms RichTextBox。 看起来当RichTextBox在表单上时, \r\n将转换为\n 。 这是一个测试:

我有两个丰富的文本框。 一个是richTextBox1 ,它放在表单上:

  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 = ""; 

另一个是rtb ,我是当场创建的。 当我运行此代码时(在窗体的load事件中):

  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 */ 

RichTextBox似乎表现出一些奇怪的行为。 当我将包含\r\n文本放入我刚刚创建的框中时,它保持不变(仍然包含\r\n )。 但是,当我将包含\r\n文本放入表单上的框中时, \r\n将变为\n

我的问题:这种行为是否有原因( \r\n – > \n )? 这种行为是否记录在某处? 我能一直指望它吗?

我在这里发布的案例是我试图了解我在其他项目中使用其中一个表单时所遇到的问题的底部,所以我很感激有关此问题的任何意见。

RichTextBox.Text属性根据RichTextBox.Rtf属性中指定的Rtf格式代码将指定的字符串转换为rtf文档。 由于’rtb’实例未被初始化,’Rtf’格式代码为空,它只是回显你的输入。 初始化’rtb’后,它包含一个空的rtf文档(带格式代码),这与’richTextBox1’的行为相同(和正确)。

结果:

 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"' 

码:

 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; 

这是Text属性工作方式的副作用。 它缓存在Control.Text中,实际的本机Windows控件在创建之前不会更新。 问题是,你的rtb从未发生过这种情况。 您没有将其添加到表单中,因此未创建本机控件。 .NET中典型的惰性资源分配模式。 因此,您正在读取缓存的值,而不是控件中的值。

要查看此内容,请修改代码以强制创建控件:

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

你会看到\ r \ n现在被翻译为\ n。 不要忘记Dispose()控件。