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
Cody gray
source share