Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataFormats Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DataFormats Class

Provides static, predefined Clipboard format names. Use them to identify the format of data that you store in an IDataObject.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Class DataFormats
Visual Basic (Usage)
Dim instance As DataFormats
C#
public class DataFormats
Visual C++
public ref class DataFormats
JScript
public class DataFormats

The IDataObject and DataObject classes also use the static format list to determine the type of data that is retrieved from the system Clipboard, or that is transferred in a drag-and-drop operation.

The GetFormat method allows you to:

  • Get a predefined DataFormats..::.Format object for a format name or ID number.

  • Add a new format name/ID number pair to the static list in this class, and to register the format with the Windows registry as a Clipboard format when you pass it the format name.

You can get the Id number or format Name from the appropriate property in the DataFormats..::.Format instance.

The following code example creates a new data format named myFormat. The code then creates a MyNewObject which it stores in a DataObject. The DataObject is copied to the Clipboard.

Next, the DataObject is retrieved from the Clipboard and the MyNewObject is recovered. The value of the MyNewObject is printed in a text box. This code requires that textBox1 has been created and placed on a form.

Visual Basic
Option Explicit
Option Strict

Imports System
Imports System.Windows.Forms

Public Class MyClass1
    Inherits Form
    Private textBox1 As TextBox

    Public Sub MyClipboardMethod()
        ' Creates a new data format.
        Dim myFormat As DataFormats.Format = _
            DataFormats.GetFormat("myFormat")

        ' Creates a new object and store it in a DataObject using myFormat 
        ' as the type of format. 
        Dim myObject As New MyNewObject()
        Dim myDataObject As New DataObject(myFormat.Name, myObject)

        ' Copies myObject into the clipboard.
        Clipboard.SetDataObject(myDataObject)

        ' Performs some processing steps.
        ' Retrieves the data from the clipboard.
        Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject()

        ' Converts the IDataObject type to MyNewObject type. 
        Dim myDereferencedObject As MyNewObject = _
            CType(myRetrievedObject.GetData(myFormat.Name), MyNewObject)

        ' Print the value of the Object in a textBox.
        textBox1.Text = myDereferencedObject.MyObjectValue
    End Sub 'MyClipboardMethod
End Class 'MyClass


' Creates a new type.
<Serializable()> Public Class MyNewObject
    Inherits Object
    Private myValue As String


    ' Creates a default constructor for the class.
    Public Sub New()
        myValue = "This is the value of the class"
    End Sub 'New

    ' Creates a property to retrieve or set the value.

    Public Property MyObjectValue() As String
        Get
            Return myValue
        End Get
        Set
            myValue = value
        End Set
    End Property
End Class 'MyNewObject


C#
using System;
using System.Windows.Forms;

public class MyClass : Form {
    protected TextBox textBox1;

    public void MyClipboardMethod() {
       // Creates a new data format.
       DataFormats.Format myFormat = DataFormats.GetFormat("myFormat");

       /* Creates a new object and stores it in a DataObject using myFormat 
        * as the type of format. */
       MyNewObject myObject = new MyNewObject();
       DataObject myDataObject = new DataObject(myFormat.Name, myObject);

       // Copies myObject into the clipboard.
       Clipboard.SetDataObject(myDataObject);

       // Performs some processing steps.

       // Retrieves the data from the clipboard.
       IDataObject myRetrievedObject = Clipboard.GetDataObject();

       // Converts the IDataObject type to MyNewObject type. 
       MyNewObject myDereferencedObject = (MyNewObject)myRetrievedObject.GetData(myFormat.Name);

       // Prints the value of the Object in a textBox.
       textBox1.Text = myDereferencedObject.MyObjectValue;
    }
 }

 // Creates a new type.
 [Serializable]
 public class MyNewObject : Object {
    private string myValue;

    // Creates a default constructor for the class.
    public MyNewObject() {
       myValue = "This is the value of the class";
    }

    // Creates a property to retrieve or set the value.
    public string MyObjectValue {
       get {
          return myValue;
       }
       set {
          myValue = value;
       }
    }
 }



Visual C++
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

// Creates a new type.

[Serializable]
public ref class MyNewObject: public Object
{
private:
   String^ myValue;

public:

   // Creates a default constructor for the class.
   MyNewObject()
   {
      myValue = "This is the value of the class";
   }


   property String^ MyObjectValue 
   {

      // Creates a property to retrieve or set the value.
      String^ get()
      {
         return myValue;
      }

      void set( String^ value )
      {
         myValue = value;
      }

   }

};

public ref class MyClass: public Form
{
protected:
   TextBox^ textBox1;

public:
   void MyClipboardMethod()
   {

      // Creates a new data format.
      DataFormats::Format^ myFormat = DataFormats::GetFormat( "myFormat" );

      /* Creates a new object and stores it in a DataObject using myFormat 
               * as the type of format. */
      MyNewObject^ myObject = gcnew MyNewObject;
      DataObject^ myDataObject = gcnew DataObject( myFormat->Name,myObject );

      // Copies myObject into the clipboard.
      Clipboard::SetDataObject( myDataObject );

      // Performs some processing steps.
      // Retrieves the data from the clipboard.
      IDataObject^ myRetrievedObject = Clipboard::GetDataObject();

      // Converts the IDataObject type to MyNewObject type. 
      MyNewObject^ myDereferencedObject = dynamic_cast<MyNewObject^>(myRetrievedObject->GetData( myFormat->Name ));

      // Prints the value of the Object in a textBox.
      textBox1->Text = myDereferencedObject->MyObjectValue;
   }

};


JScript
import System;
import System.Windows.Forms;

public class MyClass extends Form {
    protected var textBox1 : TextBox;

    public function MyClipboardMethod() {
       // Create a new data format.
       var myFormat : DataFormats.Format = DataFormats.GetFormat("myFormat");

       /* Create a new object and store it in a DataObject import the myFormat 
        * as the type of format. */
       var myObject : MyNewObject = new MyNewObject();
       var myDataObject : DataObject = new DataObject("myFormat", myObject);

       // Copy myObject into the clipboard.
       Clipboard.SetDataObject(myDataObject);

       // Perform some processing steps.

       // Retrieve the data from the clipboard.
       var myRetrievedObject : IDataObject = Clipboard.GetDataObject();

       // Convert the IDataObject type to MyNewObject type. 
       var myDereferencedObject : MyNewObject = MyNewObject(myRetrievedObject.GetData("myFormat"));

       // Print the value of the Object in a textBox.
       textBox1.Text = myDereferencedObject.MyObjectValue;
    }
 }

 // Create a new type.
Serializable public class MyNewObject extends Object {
    private var myValue : String;

    // Create a default constructor for the class.
    public function MyNewObject() {
       myValue = "This is the value of the class";
    }

    // Create a property to retrieve or set the value.
    public function get MyObjectValue() : String {
          return myValue;
    }

    public function set MyObjectValue(value : String) {
          myValue = value;
    }
 }


System..::.Object
  System.Windows.Forms..::.DataFormats
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker