Export (0) Print
Expand All

DataObject.SetData Method (Object)

Adds the specified object to the DataObject using the object type as the data format.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual void SetData(
	Object data
)

Parameters

data
Type: System.Object

The data to store.

Implements

IDataObject.SetData(Object)

If you do not know the format of the target application, you can store data in multiple formats using this method. Data stored using this method can be converted to a compatible format when it is retrieved.

The SetData(Object) overload stores the data value in a format that it determines by calling the Object.GetType method. If data implements the ISerializable interface, this overload also stores the value in the Serializable format.

The following code example stores data in a DataObject. First, a new DataObject is created and a component is stored in it. Then, the data is retrieved by specifying the class. The result is displayed in a text box.

This code requires that textBox1 has been created.

private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();

    // Creates a new data object.
    DataObject myDataObject = new DataObject();

    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);

    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
    

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft