2011-08-15 7 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'가 초기화 된 후 'richTextBox1'과 동일한 (올바른) 동작 인 빈 rtf 문서 (형식 코드 포함)가 포함됩니다.

결과 :

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

그리고 그것은 개행 문자가'\ n' 인 RTF 스펙의 일부분인가? – NickAldwin

+4

나는 그것이 rtf 파일 문자 세트가 일반적으로 '\ ansi'이고 getter가 사용하는 유니 코드 코드 페이지 '\ 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; 

그리고 당신은 N을 \로 번역되어 지금 \ r에 \를 볼 수 N 있습니다. 컨트롤을 삭제하는 것을 잊지 마세요().

+4

좋아요, 그 말이 맞는 ...하지만 '\ r \ n'이 (가) '\ n'(으)로 변경된 이유에 더 관심이있었습니다. – NickAldwin

+4

그건 그냥 컨트롤이 작동하는 방법입니다. RTB에는 많은 마일리지가 있습니다. –