Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy both - HTML and text to the clipboard?

I'm trying to put in the clipboard piece of HTML and plain text at the same time, so that HTML-capable editors could paste HTML, and other editors could use plain text.

Clipboard.SetData(DataFormats.Html, htmlWithHeader);
Clipboard.SetData(DataFormats.UnicodeText, plainText);

But only the last format is actually put to the clipboard. In the sample above, clipboard would contain only plaintext (as shown by Clipboard.GetDataObject().GetFormats()). And if I swap the lines, the clipboard would have only the HTML format.

How can I put both formats into the clipboard at the same time?

like image 507
alex Avatar asked Apr 29 '13 19:04

alex


People also ask

How do I copy and paste HTML?

Press CTRL + C. Select "Copy" from the Edit menu in your browser. Right click to display the context menu and select the "Copy" command.

How do you copy multiple items to clipboard?

Copy and paste multiple items using the Office ClipboardSelect the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.


1 Answers

You can NOT use Clipboard.SetData for setting both HTML and plain text. The second call of SetData will clear the content of clipboard that has been set by first call and store the new data.

You should use DataObject and Clipboard.SetDataObject().

Example:

DataObject dataObj = new DataObject();
dataObj.SetData(DataFormats.Html, htmlWithHeader);
dataObj.SetData(DataFormats.Text, plainText);

Clipboard.SetDataObject(dataObj);
like image 145
Akram Berkawy Avatar answered Sep 18 '22 06:09

Akram Berkawy