问题描述
如何以及它的格式复制一个RichTextBox文本到写字板或网页浏览器?
How to copy the text in a RichTextBox along with its formatting to a wordpad or webbrowser?
推荐答案
就像使用复制纯文本,你可以使用的。这将清除Windows剪贴板的当前内容,并添加指定的文本给它。
Just like with copying plain text, you would use the Clipboard.SetText
method. This clears the current contents of the Windows clipboard and adds the specified text to it.
要复制格式的文本,你需要使用一个接受该方法的超载<$c$c>TextDataFormat参数。这允许您指定要复制到剪贴板中的文本格式。在这种情况下,你会指定 TextDataFormat.Rtf
,或文字组成的富文本格式的数据。
To copy formatted text, you need to use the overload of that method that accepts a TextDataFormat
parameter. That allows you to specify the format of the text that you wish to copy to the clipboard. In this case, you would specify TextDataFormat.Rtf
, or text consisting of rich text format data.
当然,对于这个工作,你也必须使用<$c$c>Rtf在的RichTextBox
控件的属性提取与RTF格式的文本。不能使用常规的<$c$c>Text属性的,因为它不包括RTF格式信息。由于文件警告说:
Of course, for this to work, you will also have 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 the RTF formatting information. As the documentation warns:
在文本
属性不返回有关应用于的RichTextBox
的内容格式的任何信息。要获得丰富文本格式(RTF)codeS,使用 RTF
属性。
样品code:
Sample code:
' 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)
而一旦文本在剪贴板中,您(或您的应用程序的用户)可以将其粘贴你喜欢的地方。要以编程方式粘贴文本,您将使用 Clipboard.GetText
方法还接受 TextDataFormat
参数。例如:
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.GetText
method that also accepts a 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
这篇关于伴随着从一个RichTextBox其格式复制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!