IDataObject.SetData Method (String, Object)


Stores the specified data and its associated format in this instance.

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

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.

For an implementation of this method, see DataObject.SetData.

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. First, it creates a data object (myDataObject) and stores a string in the object specifying the UnicodeText format. Then it retrieves that data stored in the object specifying the Text format, so that the data is converted to the Text format. The result is displayed in a message box. This example assumes that you have created a Form named Form1.

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

    // Stores a string, specifying the UnicodeText format.
    myDataObject.SetData(DataFormats.UnicodeText, "Hello World!");

    // Retrieves the data by specifying the Text format.
    string myMessageText = "The data type is " + myDataObject.GetData(DataFormats.Text).GetType().Name;

    // Displays the result.
    MessageBox.Show(myMessageText, "The Test Result");

.NET Framework
Available since 1.1
Return to top