GetDataPresent Method (String, Boolean)
Collapse the table of content
Expand the table of content

DataObject.GetDataPresent Method (String, Boolean)

 

Determines whether this DataObject contains data in the specified format or, optionally, contains data that can be converted to the specified format.

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

public virtual bool GetDataPresent(
	string format,
	bool autoConvert
)

Parameters

format
Type: System.String

The format to check for. See DataFormats for predefined formats.

autoConvert
Type: System.Boolean

true to determine whether data stored in this DataObject can be converted to the specified format; false to check whether the data is in the specified format.

Return Value

Type: System.Boolean

true if the data is in, or can be converted to, the specified format; otherwise, false.

Call this method to determine whether a format exists before calling GetData. Call GetFormats for the formats that are available in this DataObject.

This method returns true when:

  • The autoConvert parameter is true and the data is in a format that can be converted to the appropriate format.

  • The autoConvert parameter is false and the data is in the appropriate format.

This method returns false when:

  • The autoConvert parameter is true and this method cannot find data in the specified format, and it cannot convert data to the specified format, or the data was stored with automatic conversion set to false.

  • The autoConvert parameter is false and data does not exist in this DataObject in the specified format.

System_CAPS_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.

System_CAPS_noteNote

If no data can be retrieved, no exception will be thrown. Instead, false will be returned.

The following code example determines whether data currently stored in the DataObject is associated with a specified format. First, a new DataObject is initialized with a string, specifying its format as text.

Then the DataObject is queried for data associated with the text format, specifying the autoConvert parameter as false. The result of this query is printed in a text box.

Then the DataObject is queried for data associated with string format, specifying the autoConvert parameter as true. The results are printed in the text box. This code requires that textBox1 has been created.

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

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

    // Prints the string in a text box with autoconvert = true.
    textBox1.Text += "With autoconvert = true, you can convert text to string format. " +
       "String is: " + myDataObject.GetData("System.String", true).ToString();
 }

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft