.net 从 RichTextBox 复制文本及其格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4905319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying text along with its formatting from a RichTextBox
提问by SpongeBob SquarePants
How to copy the text in a RichTextBox along with its formatting to a wordpad or webbrowser?
如何将 RichTextBox 中的文本及其格式复制到写字板或网络浏览器?
回答by Cody Gray
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.
就像复制纯文本一样,您可以使用Clipboard.SetText方法。这将清除 Windows 剪贴板的当前内容并将指定的文本添加到其中。
To copy formatted text, you need to use the overload of that methodthat 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 specify TextDataFormat.Rtf, or text consisting of rich text format data.
要复制格式化文本,您需要使用接受参数的该方法的重载TextDataFormat。这允许您指定要复制到剪贴板的文本格式。在这种情况下,您将指定TextDataFormat.Rtf, 或由富文本格式数据组成的文本。
Of course, for this to work, you will also have to use the Rtfpropertyof the RichTextBoxcontrol to extract its text with RTF formatting. You cannot use the regular Textpropertybecause it does not include the RTF formatting information. As the documentation warns:
当然,对于这个工作,你也必须使用Rtf属性的的RichTextBox控制与RTF格式来提取其文本。您不能使用常规Text属性,因为它不包括 RTF 格式信息。正如文档所警告的那样:
The
Textproperty does not return any information about the formatting applied to the contents of theRichTextBox. To get the rich text formatting (RTF) codes, use theRtfproperty.
该
Text属性不返回有关应用于RichTextBox. 要获取富文本格式 (RTF) 代码,请使用该Rtf属性。
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)
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.GetTextmethodthat also accepts a TextDataFormatparameter. For example:
一旦文本出现在剪贴板上,您(或您的应用程序的用户)就可以将其粘贴到您喜欢的任何位置。要以编程方式粘贴文本,您将使用同样接受参数的Clipboard.GetText方法TextDataFormat。例如:
' 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
回答by UncleSmokinJoe
I had a similar situation where I was copying from my VB .net application and had tried \r\n, \r, \n, vbCrLf, Chr(13), Chr(10), Chr(13) & Chr(10), etc. The new lines would appear if I pasted into Word or Wordpad, but not into Notepad. Finally I used ControlChars.NewLine where I had been using vbCrLf, and it worked. So, to summarize: Clipboard.SetText("This is one line" & ControlChars.Newline & "and this bad boy is the second.") And that works correctly. Hope it works for you!
我有一个类似的情况,我从我的 VB .net 应用程序复制并尝试了 \r\n, \r, \n, vbCrLf, Chr(13), Chr(10), Chr(13) & Chr(10)等。如果我粘贴到 Word 或 Wordpad 中,而不是记事本中,则会出现新行。最后,我在使用 vbCrLf 的地方使用了 ControlChars.NewLine,它奏效了。所以,总结一下: Clipboard.SetText("This is one line" & ControlChars.Newline & "and this bad boy is the second.") 而且它工作正常。希望这对你有用!
回答by WarpEnterprises
I used this simple event handlers (which use the built-in copy/paste methods of the richtextbox) to avoid checking for TextDataFormat:
我使用了这个简单的事件处理程序(使用富文本框的内置复制/粘贴方法)来避免检查 TextDataFormat:
private void mnuCopy_Click(object sender, EventArgs e)
{
txtRichtext.Copy();
}
private void mnuPaste_Click(object sender, EventArgs e)
{
txtRichtext.Paste();
}
回答by Alex Jolig
This is a better 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);

