Append RTF text in a RichTextBox

  • Thread starter Thread starter Olivier
  • Start date Start date
Ahh, I see. Maybe you want to set the Rtf property?

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemWindowsFormsRichTextB
oxClassRtfTopic.htm
http://msdn.microsoft.com/library/e...ystemWindowsFormsRichTextBoxClassRtfTopic.asp
http://www.devhood.com/messages/message_view-2.aspx?thread_id=91124

To append, I would assume just doing
rtb.Rtf += RtfToAppend;

would work.

If you do not have the actual RTF text, then this may be an easier way: you
can apply formats to the text, then append the text, then unapply the
formats.

For example, you can do this to turn on boldface:
System.Drawing.Font currentFont = this.richTextBox1.SelectionFont;
this.richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
FontStyle.Bold
);
this.richTextBox1.AppendText("This text appears in bold");


Then as you append text, it is bolded.
You can apply the same approach to turn on Italic, bullets, indentation,
etc.

For colors, it is similar:
this.richTextBox1.SelectionColor =
System.Drawing.Color.FromName("Green");
this.richTextBox1.AppendText("This text appears in green");

this.richTextBox1.SelectionColor =
System.Drawing.Color.FromName("Black");
 
I had to address the same problem today. I found this workaround. Basically, let Windows to the work for you by copying and pasting into a temporary Rich Text Box. This example copies the contents of RichTextBox1 and RichTextBox 2 to the clipboard. (Vb.net 2003)
Code:
    Public Sub copyText()
            Dim rtbTmp As New RichTextBox ' Temporary Rich Text Box
            Dim datobj As New System.Windows.Forms.DataObject

            ' Copy RichTextBox1 into rtbTmp
            rtbTmp.Rtf = RichTextBox1.Rtf
            ' Add Line Feed (if you want one)
            rtbTmp.AppendText(vbCrLf)  
            ' Copy RichTextBox2 to the Clipboard
            datobj.SetData(DataFormats.Rtf, RichTextBox2.Rtf) 
            Clipboard.SetDataObject(datobj)
            ' Paste RichTextBox2's copied text to the end of Temporary Rich Text Box
            rtbTmp.SelectionStart = rtbTmp.TextLength
            rtbTmp.Paste()
            ' Copy combined contents of Temporary Rich Text Box to the clipboard
            datobj.SetData(DataFormats.Rtf, rtbTmp.Rtf)
            Clipboard.SetDataObject(datobj)
    End Sub
 
Last edited:
Back
Top