2014-07-25 4 views
0

richtextBox1, richTextBox2 및 richTextBox3 : RichTextBoxes가 3 개 있습니다.2 RichTextboxes의 서식있는 텍스트를 C#의 다른 RichTextBox에 추가

나는 응용 프로그램을 실행하고 텍스트 상자 1, 2

그래서 RichTextBox1에 대한 텍스트는 "테스트"입니다에 텍스트를 입력하고 richTextBox2은 "보내고"입니다. 내가 지금 함께 텍스트를 추가하고 다른를 RichTextBox에 넣어하려는

(밑줄, 굵게 등의 서식을 보존 등)

그래서 나는 다음과 같은 코드를 시도 :

richTextBox3.Rtf = richTextBox1.Rtf + richTextBox2.Rtf; 

을 이 오류가 발생하지 않지만 richTextBox1 텍스트 만 가져옵니다. 그래서 나는 "Test"를 얻는다.

형식을 유지하면서 2 RichTextBoxes의 내용을 어떻게 복사합니까?

따 당신은이 작업을 수행 할 것

답변

1

: 트릭을 할해야

richTextBox3.Rtf = richTextBox1.Rtf 
richTextBox3.Select(richTextBox3.TextLength, 0); 
richTextBox3.SelectedRtf = richTextBox2.Rtf; 

.

+0

감사합니다. 필요에 따라 작동합니다. – Sun

1

사용법 :

richTextBox3.Rtf = MergeRtfTexts(new RichTextBox[] { richTextBox1, richTextBox2}); 

RTF 합병 기능 :

private string MergeRtfTexts(RichTextBox[] SourceRtbBoxes) 
    { 
     using (RichTextBox temp = new RichTextBox()) 
     { 
      foreach (RichTextBox rtbSource in SourceRtbBoxes) 
      { 
       rtbSource.SelectAll(); 
       //move the end 
       temp.Select(temp.TextLength, 0); 
       //append the rtf 
       temp.SelectedRtf = rtbSource.SelectedRtf; 
      } 
      return temp.Rtf; 
     } 
    }