Editar

Partilhar via


IDataObject.GetDataPresent Method

Definition

Determines whether data stored in this instance is associated with the specified format.

Overloads

GetDataPresent(String)

Determines whether data stored in this instance is associated with, or can be converted to, the specified format.

GetDataPresent(Type)

Determines whether data stored in this instance is associated with, or can be converted to, the specified format.

GetDataPresent(String, Boolean)

Determines whether data stored in this instance is associated with the specified format, using a Boolean value to determine whether to convert the data to the format.

GetDataPresent(String)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Determines whether data stored in this instance is associated with, or can be converted to, the specified format.

public:
 bool GetDataPresent(System::String ^ format);
public bool GetDataPresent (string format);
abstract member GetDataPresent : string -> bool
Public Function GetDataPresent (format As String) As Boolean

Parameters

format
String

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

Returns

true if data stored in this instance is associated with, or can be converted to, the specified format; otherwise false.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. First, it creates a data object using a string and the Text format. Then it verifies that the data is present in the Text format and displays the results in a message box. The example assumes that you have created a Form named Form1.

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

      // Checks whether the data is present in the Text format and displays the result.
      if ( myDataObject->GetDataPresent( DataFormats::Text ) )
            MessageBox::Show( "The stored data is in the Text format.", "Test Result" );
      else
            MessageBox::Show( "The stored data is not in the Text format.", "Test Result" );
   }
       private void TestDataObject() 
       {
           // Creates a new data object using a string and the Text format.
           string myString = "Hello World!";
           DataObject myDataObject = new DataObject(DataFormats.Text, myString);

           // Checks whether the data is present in the Text format and displays the result.
           if (myDataObject.GetDataPresent(DataFormats.Text))
               MessageBox.Show("The stored data is in the Text format." , "Test Result");
           else
               MessageBox.Show("The stored data is not in the Text format.", "Test Result");
       }
Private Sub TestDataObject()
    ' Creates a new data object using a string and the Text format.
    Dim myString As New String("Hello World!")
    Dim myDataObject As New DataObject(DataFormats.Text, myString)

    ' Checks whether the data is present in the Text format and displays the result.
    If (myDataObject.GetDataPresent(DataFormats.Text)) Then
        MessageBox.Show("The stored data is in the Text format.", "Test Result")
    Else
        MessageBox.Show("The stored data is not in the Text format.", "Test Result")
    End If
End Sub

Remarks

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

Note

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

See also

Applies to

GetDataPresent(Type)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Determines whether data stored in this instance is associated with, or can be converted to, the specified format.

public:
 bool GetDataPresent(Type ^ format);
public bool GetDataPresent (Type format);
abstract member GetDataPresent : Type -> bool
Public Function GetDataPresent (format As Type) As Boolean

Parameters

format
Type

A Type representing the format for which to check. See DataFormats for predefined formats.

Returns

true if data stored in this instance is associated with, or can be converted to, the specified format; otherwise, false.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. First, it creates a component (myComponent) and stores it in a data object (myDataObject). Then it checks whether the specified data is stored in myDataObject. If the test evaluates true, it displays the result in a message box and displays the data type in a text box. This example assumes that you have already created a Form named Form1 and a TextBox named textBox1.

private:
   void GetDataPresent2()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;

      // Creates a new data object and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject( myComponent );

      // Creates a type to store the type of data.
      Type^ myType = myComponent->GetType();

      // Checks whether the specified data type exists in the object.
      if ( myDataObject->GetDataPresent( myType ) )
      {
         MessageBox::Show( "The specified data is stored in the data object." );

         // Displays the type of data.
         textBox1->Text = "The data type is " + myDataObject->GetData( myType )->GetType()->Name + ".";
      }
      else
            MessageBox::Show( "The specified data is not stored in the data object." );
   }
       private void GetDataPresent2() 
       {
           // Creates a component to store in the data object.
           Component myComponent = new Component();

           // Creates a new data object and assigns it the component.
           DataObject myDataObject = new DataObject(myComponent);

           // Creates a type to store the type of data.
           Type myType = myComponent.GetType();

           // Checks whether the specified data type exists in the object.
           if (myDataObject.GetDataPresent(myType))
           {
               MessageBox.Show("The specified data is stored in the data object.");
               // Displays the type of data.
               textBox1.Text = "The data type is " + myDataObject.GetData(myType).GetType().Name + ".";
           }
           else
           {
               MessageBox.Show("The specified data is not stored in the data object.");
           }
       }
Private Sub GetDataPresent2()
    ' Creates a component to store in the data object.
    Dim myComponent As New System.ComponentModel.Component()

    ' Creates a new data object and assigns it the component.
    Dim myDataObject As New DataObject(myComponent)

    'Creates a type to store the type of data.
    Dim myType As Type = myComponent.GetType()

    ' Checks whether the specified data type exists in the object.
    If myDataObject.GetDataPresent(myType) Then
        MessageBox.Show("The specified data is stored in the data object.")
        ' Displays the type of data.
        TextBox1.Text = "The data type is " & myDataObject.GetData(myType).GetType().Name & "."
    Else
        MessageBox.Show("The specified data is not stored in the data object.")
    End If
End Sub

Remarks

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

Note

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

See also

Applies to

GetDataPresent(String, Boolean)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Determines whether data stored in this instance is associated with the specified format, using a Boolean value to determine whether to convert the data to the format.

public:
 bool GetDataPresent(System::String ^ format, bool autoConvert);
public bool GetDataPresent (string format, bool autoConvert);
abstract member GetDataPresent : string * bool -> bool
Public Function GetDataPresent (format As String, autoConvert As Boolean) As Boolean

Parameters

format
String

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

autoConvert
Boolean

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

Returns

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

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. First, it creates a data object (myDataObject) using a string and the Text format. Then it queries the object for data associated with the Text format, with the autoConvert parameter set to false. This trial fails, and the result is displayed in a message box labeled "Message #1." In the second trial, it sets the autoConvert parameter 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 GetDataPresent3()
   {
      // Creates a new data object using a string and the Text format.
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,"My String" );

      // Checks whether the string can be displayed with autoConvert equal to false.
      if ( myDataObject->GetDataPresent( "System::String", false ) )
            MessageBox::Show( myDataObject->GetData( "System::String", false )->ToString(), "Message #1" );
      else
            MessageBox::Show( "Cannot convert data to the specified format with autoConvert set to false.", "Message #1" );

      // Displays the string with autoConvert equal to true.
      MessageBox::Show( "Now that autoConvert is true, you can convert " + myDataObject->GetData( "System::String", true ) + " to string format.", "Message #2" );
   }
       private void GetDataPresent3() 
       {
           // Creates a new data object using a string and the Text format.
           DataObject myDataObject = new DataObject(DataFormats.Text, "My String");

           // Checks whether the string can be displayed with autoConvert equal to false.
           if(myDataObject.GetDataPresent("System.String", false)) 
               MessageBox.Show(myDataObject.GetData("System.String", false).ToString(), "Message #1");
           else
               MessageBox.Show("Cannot convert data to the specified format with autoConvert set to false.", "Message #1");

           // Displays the string with autoConvert equal to true.
           MessageBox.Show("Now that autoConvert is true, you can convert " + 
               myDataObject.GetData("System.String", true).ToString() + " to string format.","Message #2");
       }
Private Sub GetDataPresent3()
    ' Creates a new data object using a string and the Text format.
    Dim myDataObject As New DataObject(DataFormats.Text, "My String")

    ' Checks whether the string can be displayed with autoConvert equal to false.
    If myDataObject.GetDataPresent("System.String", False) Then
        MessageBox.Show(myDataObject.GetData("System.String", False).ToString() + ".", "Message #1")
    Else
        MessageBox.Show("Cannot convert data to the specified format with autoConvert set to false.", "Message #1")
    End If
    ' Displays the string with autoConvert equal to true.
    MessageBox.Show(("Now that autoConvert is true, you can convert " + myDataObject.GetData("System.String", _
         True).ToString() + " to string format."), "Message #2")

End Sub

Remarks

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

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 autoConvert set to false.

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

Note

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

See also

Applies to