RichTextBox换行转换?

Nic*_*win 13 .net c# richtextbox winforms

我正在使用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 = "";
Run Code Online (Sandbox Code Playgroud)

另一个是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
  */
Run Code Online (Sandbox Code Playgroud)

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

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

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

mar*_*ark 7

所述RichTextBox.Text属性是根据在指定的RTF格式的码的分配的字符串转换成RTF文档RichTextBox.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"'
Run Code Online (Sandbox Code Playgroud)

码:

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
    */
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为它更多地与rtf文件字符集通常为'\ ansi'和getter使用的unicode代码页'\ ansicpg1252'有关.输入文本中的换行符按rtf规范编码为rtf段落'\ par',并由unicode rtf代码页重新发布为换行符. (4认同)
  • 并且它是RTF规范的一部分,新行是`\n`? (3认同)

Han*_*ant 5

  var rtb = new RichTextBox();
  string enl = "Cheese" + Environment.NewLine + "Whiz";
  rtb.Text = enl;
Run Code Online (Sandbox Code Playgroud)

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

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

        var rtb = new RichTextBox();
        rtb.CreateControl();
        string enl = "Cheese" + Environment.NewLine + "Whiz";
        rtb.Text = enl;
Run Code Online (Sandbox Code Playgroud)

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

  • 好吧,这是有道理的...但我更关心为什么`\ r \n`改为`\n`. (4认同)
  • 这就是控制的工作原理.RTB有很多里程. (4认同)