Export (0) Print
Expand All

DataObject.GetData Method (String, Boolean)

Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual Object GetData (
	string format,
	bool autoConvert
)
public Object GetData (
	String format, 
	boolean autoConvert
)
public function GetData (
	format : String, 
	autoConvert : boolean
) : Object

Parameters

format

The format of the data to retrieve. See DataFormats for predefined formats.

autoConvert

true to the convert data to the specified format; otherwise, false.

Return Value

The data associated with the specified format, or a null reference (Nothing in Visual Basic).

If the autoConvert parameter is true and this method cannot find data in the specified format, it attempts to convert the data to the format. If the data cannot be converted to the specified format, or if the data was stored with the automatic conversion set to false, this method returns a null reference (Nothing in Visual Basic).

If the autoConvert parameter is false, this method returns data in the specified format, or a null reference (Nothing in Visual Basic) if no data in this format can be found.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this DataObject.

NoteNote

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

The following code example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether to convert the data format.

First, a new DataObject is created with text data. Then the example tries to retrieve the data, specifying its format as a string and no format conversion, that is, the autoConvert parameter is false. This operation fails because there is no string data in the DataObject.

Next, the example tries to retrieve the data again, with the autoConvert parameter set to true. This operation succeeds and the results are displayed in a MessageBox.

This code requires that textBox1 has been created.

private void GetMyData3() {
    // Creates a new data object using a string and the text format.
    string myString = "My new text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);
 
    // Prints the string in a text box with autoconvert = false.
    if(myDataObject.GetData("System.String", false) != null) {
       // Prints the string in a text box.
       textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
    } else
       textBox1.Text = "Could not find data of the specified format" + '\n';
 
    // Prints the string in a text box with autoconvert = true.
    textBox1.Text += myDataObject.GetData("System.String", true).ToString();
 }
 

private void GetMyData3()
{
    // Creates a new data object using a string and the text format.
    String myString = "My new text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);

    // Prints the string in a text box with autoconvert = false.
    if (myDataObject.GetData("System.String", false) != null) {
        // Prints the string in a text box.
        textBox1.set_Text(myDataObject.GetData("System.String", false).
            ToString() + '\n');
    }
    else {
        textBox1.set_Text("Could not find data of the specified format"
            + '\n');
    }

    // Prints the string in a text box with autoconvert = true.
    textBox1.set_Text(textBox1.get_Text() + myDataObject.GetData(
        "System.String", true).ToString());
} //GetMyData3

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:
© 2014 Microsoft