Copy text along with formatting from RichTextBox

How to copy text to RichTextBox along with its formatting to a text block or web browser?

+6
clipboard winforms richtextbox
source share
4 answers

As with copying plain text, you should use the Clipboard.SetText method. This clears the current contents of the Windows clipboard and appends the specified text to it.

To copy formatted text, you need to use the overload of this method , which takes TextDataFormat . This allows you to specify the format of the text you want to copy to the clipboard. In this case, you must specify TextDataFormat.Rtf or text consisting of data with an expanded text format.

Of course, for this you will also need to use the Rtf property of the RichTextBox control to extract its text with RTF formatting. You cannot use the regular Text property because it does not include RTF formatting information. As the documentation warns:

The Text property does not return any formatting information applied to the contents of a RichTextBox . To get rich text formatting (RTF) codes, use the Rtf property.


Code example:

 ' Get the text from your rich text box Dim textContents As String = myRichTextBox.Rtf ' Copy the text to the clipboard Clipboard.SetText(textContents, TextDataFormat.Rtf) 


And as soon as the text is on the clipboard, you (or the user of your application) can paste it wherever you want. To insert text programmatically, you will use the Clipboard.GetText method, which also accepts the TextDataFormat parameter. For example:

 ' Verify that the clipboard contains text If (Clipboard.ContainsText(TextDataFormat.Rtf)) Then ' Paste the text contained on the clipboard into a DIFFERENT RichTextBox myOtherRichTextBox.Rtf = Clipboard.GetText(TextDataFormat.Rtf) End If 
+9
source share

I had a similar situation when I copied VB.net from my application and tried \ r \ n, \ r, \ n, vbCrLf, Chr (13), Chr (10), Chr (13) and Chr (10) and etc. New lines will appear if I am pasted into Word or Wordpad, but not into Notepad. Finally, I used ControlChars.NewLine, where I used vbCrLf, and it worked. So to summarize: Clipboard.SetText (“This is one line” and “ControlChars.Newline” and “this bad boy is the second”). And it works correctly. Hope this works for you!

+2
source share

I used these simple event handlers (which use the copy / paste built-in methods for richtextbox) to avoid checking the TextDataFormat:

 private void mnuCopy_Click(object sender, EventArgs e) { txtRichtext.Copy(); } private void mnuPaste_Click(object sender, EventArgs e) { txtRichtext.Paste(); } 
+1
source share

This is the best solution (based on this answer ):

 var dto = new DataObject(); dto.SetText(richTextBox.SelectedRtf, TextDataFormat.Rtf); //Since notepad sux and doesn't understand \n, //we need to fix it replacing by Environment.NewLine (\r\n) string unformattedText = richTextBox.SelectedText.Replace("\n", Environment.NewLine); dto.SetText(unformattedText, TextDataFormat.UnicodeText); Clipboard.Clear(); Clipboard.SetDataObject(dto); 
0
source share

All Articles