Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleSave and restore data on the Clipboard in multiple formats in VB .NET
DescriptionThis example shows how to save and restore data on the Clipboard in multiple formats in VB .NET. It makes a DataObject containing the data in different formats and saves that object to the Clipboard.
KeywordsClipboard, multiple formats, formats, VB.NET
CategoriesVB.NET, Tips and Tricks
 
This example saves data in Rich Text, text, and HTML format. When you paste the data, it displays each of these formats separately.

To copy the data to the clioboard, the btnCopy_Click event handler makes a new DataObject. It calls the data object's SetData method to save data in Rich Text and text formats. It then composes an HTML document and uses the data object's SetData method to save that data in the HTML format.

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

 
Private Sub btnCopy_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCopy.Click
    ' Make a DataObject.
    Dim data_object As New DataObject

    ' Add the data in various formats.
    data_object.SetData(DataFormats.Rtf, rchSource.Rtf)
    data_object.SetData(DataFormats.Text, rchSource.Text)

    Dim html_text As String
    html_text = "<HTML>" & vbCrLf
    html_text &= "  <HEAD>The Quick Brown Fox</HEAD>" & _
        vbCrLf
    html_text &= "  <BODY>" & vbCrLf
    html_text &= rchSource.Text & vbCrLf
    html_text &= "  </BODY>" & vbCrLf & "</HTML>"
    data_object.SetData(DataFormats.Html, html_text)

    ' Place the data in the Clipboard.
    Clipboard.SetDataObject(data_object)
End Sub
 
To paste the data, the program uses the Clipboard's GetDataObject to get the IDataObject that holds the Clipboard's data.

The code then uses the data object's GetDataPresent method to see if the object contains Rich Text, text, and HTML format data. For each format that is present, the program calls the data object's GetData method to get the data.

 
Private Sub btnPaste_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPaste.Click
    ' Get the DataObject.
    Dim data_object As IDataObject = Clipboard.GetDataObject

    If data_object.GetDataPresent(DataFormats.Rtf) Then
        rchTarget.Rtf = _
            data_object.GetData(DataFormats.Rtf).ToString
        lblRtf.Text = _
            data_object.GetData(DataFormats.Rtf).ToString
    Else
        rchTarget.Text = ""
        lblRtf.Text = ""
    End If

    If data_object.GetDataPresent(DataFormats.Text) Then
        lblTarget.Text = _
            data_object.GetData(DataFormats.Text).ToString
    Else
        lblTarget.Text = ""
    End If

    If data_object.GetDataPresent(DataFormats.Html) Then
        lblHtml.Text = _
            data_object.GetData(DataFormats.Html).ToString
    Else
        lblHtml.Text = ""
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated