[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: See what kinds of data are available in the clipboard and display them in C#

example

This example shows how you can see what kinds of data are available in the clipboard. The program's form contains four controls that can display samples of data. They include a PictureBox, TextBox, RichTextBox, and WebBrowser.

Before the WebBrowser can display HTML loaded from code, it needs a Document object. The program gives it one by navigating to the blank page when the program starts.

private void Form1_Load(object sender, EventArgs e) { wbrSample.Navigate("about:blank"); }

After navigation is complete, the code can use the control's Document property to display HTML.

When you click the List Formats button, the following code displays the available clipboard formats and shows samples of the kinds of data it can understand.

// List the available formats. private void btnListFormats_Click(object sender, EventArgs e) { IDataObject data_object = Clipboard.GetDataObject(); lstFormats.Items.Clear(); foreach (string format in data_object.GetFormats()) lstFormats.Items.Add(format); DisplayData(); }

This event handler gets the clipboard's data object and clears the program's ListBox. It then loops through the data formats returned by the data object's GetFormats method and displays them in the ListBox. It finishes by calling the following DisplayData method to display whatever data it can.

// Display data if possible. private void DisplayData() { txtSample.Visible = false; rchSample.Visible = false; picSample.Visible = false; wbrSample.Visible = false; // Image. if (Clipboard.ContainsImage()) { picSample.Image = Clipboard.GetImage(); picSample.Visible = true; } // Text. if (Clipboard.ContainsText(TextDataFormat.UnicodeText)) { txtSample.Text = Clipboard.GetText( TextDataFormat.UnicodeText); txtSample.Visible = true; } // HTML. if (Clipboard.ContainsText(TextDataFormat.Html)) { HtmlDocument doc = wbrSample.Document; doc.Body.InnerHtml = Clipboard.GetText( TextDataFormat.Html); wbrSample.Visible = true; } // Rich Text. if (Clipboard.ContainsText(TextDataFormat.Rtf)) { rchSample.Rtf = Clipboard.GetText( TextDataFormat.Rtf); rchSample.Visible = true; } }

This code hides the program's four display controls (so they don't clutter things up if there's no data for them to display). It then uses the Clipboard object's ContainsXxx methods to see what kinds of data are available on the clipboard. If the clipboard contains image, Unicode, HTML, or RTF data, the program displays it.

Notice that the code uses the clipboard's GetText method to get text, HTML, and RTF data. It passes GetText the kind of text it wants.

Take a look at the picture of the example program. Here I opened a Word file (included in the download) that contains an image and text in various fonts and colors. Note that the program doesn't say there is image data available in the clipboard. The selected text contains an image, but there is no image available by itself.

Also notice that the program shows that there are just over a dozen formats available, all provided by Word to the clipboard.

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

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