Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataObject.SetData Method (String, Boolean, Object)

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
)
public void SetData (
	String format, 
	boolean autoConvert, 
	Object data
)
public function SetData (
	format : String, 
	autoConvert : boolean, 
	data : Object
)

Parameters

format

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

autoConvert

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

data

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';
 }
 

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.set_Text("The format(s) associated with the data are: " + '\n');
    for (int i = 0; i < arrayOfFormats.get_Length(); i++) {
        textBox1.set_Text(textBox1.get_Text() + arrayOfFormats.get_Item(i) 
            + '\n');
    }
} //AddMyData4 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft