RichTextBox的换行转换? [英] RichTextBox Newline Conversion?

查看:158
本文介绍了RichTextBox的换行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的WinForms RichTextBox的。看来,当RichTextBox的是一种形式, \ r \ N于被转换为。这是一个测试:

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:

我有两个丰富的文本框。一个是 richTextBox1 ,这是摆在窗体上的:

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

另外就是 RTB ,这是我创建的地方。当我运行此code(在窗体的Load事件):

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

在RichTextBox似乎表现出一些奇怪的行为。当我把包含文本的 \ r \ñ到我刚刚创建的盒子,它保持不变(仍包含 \ r \ñ)。但是,当我把包含文本的 \ r \ñ到窗体上盒, \ r \ñ获取变成

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.

我的问题:还有一个原因是这种行为( \ r \ñ - > )?难道这种行为记录的地方?我能指望它总是被这样?

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?

我张贴在这里的情况是我尝试让一个问题我一直有与我的形式在不同的项目中的一个底部,所以我最好的AP preciate任何投入就这个问题。

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.

推荐答案

的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.text.aspx">RichTextBox.Text属性是根据RTF格式的codeS在<指定的转换指定字符串转换为RTF文档href="http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf.aspx">RichTextBox.Rtf属性。由于RTB实例没有初始化的RTF格式codeS是空的,它只是回显您的输入。经过RTB初始化它包含一个空的RTF文档(与格式codeS),这是相同的(正确的)行为是richTextBox1。

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

结果:

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:

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

这篇关于RichTextBox的换行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆