DataObject.SetData Method (String, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

public virtual void SetData(
	string format,
	object data


Type: System.String

The format associated with the data. See DataFormats for predefined formats.

Type: System.Object

The data to store.

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, specifying its format as Unicode.

Then the data is retrieved by specifying the text format, since the default is to convert the data when the final format is compatible. The result is displayed in a text box.

This code requires that textBox1 has been created.

private void AddMyData() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject();

    // Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string");

    // Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name;

.NET Framework
Available since 1.1
Return to top