DataObject.SetData Method (String, Boolean, 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 and indicating whether the data can be converted to another format.

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

public virtual void SetData(
	string format,
	bool autoConvert,
	object data


Type: System.String

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

Type: System.Boolean

true to allow the data to be converted to another format; otherwise, false.

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.

The following code example stores data in a DataObject and specifies that the data can only be retrieved in its native format.

First, a new DataObject is created. Data in the Unicode format is stored in the DataObject, with autoConvert set to false.

Then, the DataObject is queried for the list of available data formats. Only the Unicode format is returned, although Unicode data can be converted to text and other formats.

This code requires that textBox1 has been created.

private void AddMyData4() {
    // Creates a new data object, and assigns it the component.
    DataObject myDataObject = new DataObject();

    // Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, false, "My Unicode data");

    // Gets the data formats in the DataObject.
    String[] arrayOfFormats = myDataObject.GetFormats();

    // Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " + '\n';
    for(int i=0; i<arrayOfFormats.Length; i++)
       textBox1.Text += arrayOfFormats[i] + '\n';

.NET Framework
Available since 1.1
Return to top