Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SetData Method (Type, Object)

DataObject.SetData Method (Type, Object)

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

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

public virtual void SetData(
	Type format,
	Object data
)

Parameters

format
Type: System.Type

A Type representing the format associated with the data.

data
Type: System.Object

The data to store.

Implements

IDataObject.SetData(Type, 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 following code example stores data in a DataObject using a Type as the data format. The data is then retrieved by calling GetData using the Type to specify the data format. The result is displayed in a text box.

This code requires that textBox1 has been created.

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

    // Gets the type of the component.
    Type myType = myComponent.GetType();

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

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

    // Prints whether data of the specified type is in the DataObject. 
    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.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft