2011-08-15 78 views
12

我正在使用WinForms RichTextBox。看起來,當RichTextBox在窗體上時,\r\n被轉換爲\n。這裏有一個測試:RichTextBox換行符?

我有兩個富文本框。一個是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)?這種行爲是否記錄在某處?我可以依靠它總是這樣嗎?

我在這裏發佈的案例是我試圖弄清楚我在一個不同的項目中使用我的表單時遇到的問題的底部,因此我非常感謝關於此問題的任何輸入。

+1

請記住以前看過這個,但是我只是繞過它: - /期待看到答案 – GreyCloud

回答

7

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

並且它是RTF規範的一部分,換行符是'\ n'? – NickAldwin

+4

我相信它跟rtf文件字符集通常是'\ ansi'和getter使用的unicode代碼頁'\ ansicpg1252'有關。輸入文本中的換行符被編碼爲rtf段落,按照rtf規範中斷'\ par',並由unicode rtf代碼頁重新發送爲換行符。 – mark

+0

向上面的代碼添加了一個MessageBox,它演示了段落編碼。 – mark

5
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()控件。

+4

好的,這很有道理......但我更關心爲什麼將\ r \ n更改爲\ n。 – NickAldwin

+4

這就是控制的工作原理。實時出價有很多里程。 –