伴随着从一个RichTextBox其格式复制文本 [英] Copying text along with its formatting from a RichTextBox

查看:194
本文介绍了伴随着从一个RichTextBox其格式复制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以及它的格式复制一个RichTextBox文本到写字板或网页浏览器?

How to copy the text in a RichTextBox along with its formatting to a wordpad or webbrowser?

推荐答案

就像使用复制纯文本,你可以使用的 Clipboard.SetText 方法。这将清除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.

要复制格式的文本,你需要使用一个接受该方法的超载<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textdataformat.aspx"><$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.

当然,对于这个工作,你也必须使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf.aspx"><$c$c>Rtf的RichTextBox 控件的属性提取与RTF格式的文本。不能使用常规的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.text.aspx"><$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 属性。

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


样品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其格式复制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆