IDataObject::GetData Method (String^, Boolean)

 

Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.

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

Object^ GetData(
	String^ format,
	bool autoConvert
)

Parameters

format
Type: System::String^

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

autoConvert
Type: System::Boolean

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

Return Value

Type: System::Object^

The data associated with the specified format, or null.

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 autoConvert parameter set to false, this method returns null.

If the autoConvert parameter is false, this method returns data in the specified format, or null 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 instance.

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.

For an implementation of this method, see DataObject::GetData.

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. The example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether or not to convert the data format. First, myDataObject is created with text data. Then the example tries twice to retrieve the data. In the first trial, it specifies its format as a string and sets the autoConvert parameter to false. This trial fails, and the result is displayed in a message box labeled "Message #1." In the second trial, the example retrieves the same data with the autoConvert parameter set to true. This trial succeeds, and the result is displayed in a message box labeled "Message #2." The example assumes that you have created a Form named Form1.

private:
   void GetData3()
   {
      // Creates a new data object using a text string.
      String^ myString = "Hello World!";
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,myString );

      // Displays the string with autoConvert equal to false.
      if ( myDataObject->GetData( "System::String", false ) != nullptr )
      {
         // Displays the string in a message box.
         MessageBox::Show( myDataObject->GetData( "System::String", false ) + ".", "Message #1" );
      }
      else
            MessageBox::Show( "Could not find data of the specified format.", "Message #1" );

      // Displays a not found message in a message box.
      // Displays the string in a text box with autoConvert equal to true.
      String^ myData = "The data is " + myDataObject->GetData( "System::String", true ) + ".";
      MessageBox::Show( myData, "Message #2" );
   }

.NET Framework
Available since 1.1
Return to top
Show: