c# - RichTextBox换行转换?

标签 c# .net winforms 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,是我现场制作的。当我运行此代码时(在表单的加载事件中):

  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 文档属性(property)。由于“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
    */
}

关于c# - RichTextBox换行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7067899/

相关文章:

.net - 如何将.NET TableCell渲染为TH而不是TD?

c# - ListView 光标变化和闪烁

c# - 检查串口是否打开

c# - 如何在 C# 中遍历类的实例?

c# - 我应该在每次交易后关闭套接字(TCPIP)吗?

c# - 使用 OData V4 客户端更新动态实体

c# - 数组在内存中是如何处理的?

.net - Jenkins Amazon EC2 代理云 - Windows 从属

c# - C# 中的 Windows 资源管理器/Aero 样式工具提示?

c# - 在 WebBrowser 控件中打印水印背景图像