[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Copy and paste data in multiple formats to the clipboard in C#

Copy and paste data in multiple formats to the clipboard

This example shows how to copy and paste data in multiple formats to the clipboard. It saves data in Rich Text (RTF), text, and HTML formats. When you paste the data, it separately displays each of those formats that are available.

The following code shows how the program copies data to the clipboard.

// Copy data to the clipboard in text, RTF, and HTML formats. private void btnCopy_Click(object sender, EventArgs e) { // Make a DataObject. DataObject data_object = new DataObject(); // Add the data in various formats. data_object.SetData(DataFormats.Rtf, rchSource.Rtf); data_object.SetData(DataFormats.Text, rchSource.Text); string html_text; html_text = "\r\n"; html_text += " The Quick Brown Fox\r\n"; html_text += " \r\n"; html_text += rchSource.Text + "\r\n"; html_text += " \r\n"; html_text += ""; data_object.SetData(DataFormats.Html, html_text); // Place the data in the Clipboard. Clipboard.SetDataObject(data_object); }

First the code makes a new DataObject. It calls the object's SetData method to save data in RTF and text formats. It then composes a small HTML document and uses the data object's SetData method again to save the document in the HTML format.

The program then calls the Clipboard object's SetDataObject method to save the DataObject on the Clipboard.

The following code shows how the program pastes data from the clipboard.

// Paste data from the clipboard in text, // RTF, and HTML formats if they are available. private void btnPaste_Click(object sender, EventArgs e) { // Get the DataObject. IDataObject data_object = Clipboard.GetDataObject(); if (data_object.GetDataPresent(DataFormats.Rtf)) { rchRtf.Rtf = data_object.GetData(DataFormats.Rtf).ToString(); txtRtfCode.Text = data_object.GetData(DataFormats.Rtf).ToString(); } else { rchRtf.Clear(); txtRtfCode.Clear(); } if (data_object.GetDataPresent(DataFormats.Text)) { txtText.Text = data_object.GetData(DataFormats.Text).ToString(); } else { txtText.Clear(); } if (data_object.GetDataPresent(DataFormats.Html)) { txtHtml.Text = data_object.GetData(DataFormats.Html).ToString(); } else { txtHtml.Clear(); } }

For each of the data RTF, text, and HTML data types, the code uses the Clipboard object's GetDataPresent method to see if the format is available. If so, the code uses the Clipboard's GetData method to get the data.

Note that this method works no matter what program placed the data in the clipboard. For example, if you use Word to copy some text, you can then paste it into this program in RTF, text, and HTML formats. Try copying the initial text from this program into Word, and then copying it back from Word to this program. The result should be similar but slightly different from what you get when you copy from this program to itself.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.