How to copy the text in a RichTextBox along with its formatting to a wordpad or webbrowser?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just like with copying plain text, you would use the
Clipboard.SetTextmethod. This clears the current contents of the Windows clipboard and adds the specified text to it.To copy formatted text, you need to use the overload of that method that accepts a
TextDataFormatparameter. That allows you to specify the format of the text that you wish to copy to the clipboard. In this case, you would specifyTextDataFormat.Rtf, or text consisting of rich text format data.Of course, for this to work, you will also have to use the
Rtfproperty of theRichTextBoxcontrol to extract its text with RTF formatting. You cannot use the regularTextproperty because it does not include the RTF formatting information. As the documentation warns:Sample code:
And once the text is on the clipboard, you (or the user of your application) can paste it wherever you like. To paste the text programmatically, you will use the
Clipboard.GetTextmethod that also accepts aTextDataFormatparameter. For example: