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
)가 있습니까? 이 동작은 어딘가에 문서화되어 있습니까? 항상 이런 식으로 생각할 수 있습니까?
여기에 게시 한 사례는 다른 프로젝트에서 내 양식 중 하나를 사용하면서 겪었던 문제의 근본적인 원인을 찾기위한 시도이므로이 문제와 관련하여 의견을 보내 주시면 감사하겠습니다.
나는 이것을 해킹했다는 것을 기억하기 전에 :-/대답을보기를 고대한다. – GreyCloud