IDataObject.GetDataPresent 方法

定义

确定此实例中存储的数据是否与指定的格式关联。Determines whether data stored in this instance is associated with the specified format.

重载

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)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。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

参数

format
String

要检查的格式。The format for which to check. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

如果此实例中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 falsetrue if data stored in this instance is associated with, or can be converted to, the specified format; otherwise false.

示例

此示例使用DataObject实现IDataObject的类来GetDataPresent演示方法的用法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. 首先, 它使用字符串和Text格式创建数据对象。First, it creates a data object using a string and the Text format. 然后, 它将验证数据是否以Text格式显示, 并将结果显示在一个消息框中。Then it verifies that the data is present in the Text format and displays the results in a message box. 该示例假设您已创建了一个FormForm1为的。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

注解

调用此方法可在调用DataObject GetData之前确定中是否存在格式。Call this method to determine whether a format exists in this DataObject before calling GetData. 调用GetFormats此实例中可用的格式。Call GetFormats for the formats that are available in this instance.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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. 例如, 以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

有关此方法的实现, 请参见DataObject.GetDataPresentFor an implementation of this method, see DataObject.GetDataPresent.

另请参阅

GetDataPresent(Type)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。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

参数

format
Type

Type 表示要检查的格式。A Type representing the format for which to check. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

如果此实例中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 falsetrue if data stored in this instance is associated with, or can be converted to, the specified format; otherwise, false.

示例

此示例使用DataObject实现IDataObject的类来GetDataPresent演示方法的用法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. 首先, 它创建一个组件 (myComponent), 并将其存储在数据对象myDataObject() 中。First, it creates a component (myComponent) and stores it in a data object (myDataObject). 然后, 它会检查指定的数据是否存储myDataObject在中。Then it checks whether the specified data is stored in myDataObject. 如果测试计算true, 它将在一个消息框中显示结果, 并在文本框中显示数据类型。If the test evaluates true, it displays the result in a message box and displays the data type in a text box. 此示例假设您已创建Form了名Form1为的和TextBoxtextBox1为的。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

注解

调用此方法可在调用DataObject GetData之前确定中是否存在格式。Call this method to determine whether a format exists in this DataObject before calling GetData. 调用GetFormats此实例中可用的格式。Call GetFormats for the formats that are available in this instance.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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. 例如, 以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

有关此方法的实现, 请参见DataObject.GetDataPresentFor an implementation of this method, see DataObject.GetDataPresent.

另请参阅

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.

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

参数

format
String

要检查的格式。The format for which to check. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean

值为 true 时,确定此实例中存储的数据是否可以转换成指定的格式;值为 false 时,检查数据的格式是否为指定的格式。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.

返回

如果数据采用指定的格式,或者可以转换成指定的格式,则为 true;否则为 falsetrue if the data is in, or can be converted to, the specified format; otherwise, false.

示例

此示例使用DataObject实现IDataObject的类来GetDataPresent演示方法的用法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetDataPresent method. 首先, 它使用字符串myDataObject Text和格式创建数据对象 ()。First, it creates a data object (myDataObject) using a string and the Text format. 然后, 它将在对象中查询与Text格式关联的数据, autoConvert并将参数设置为。 falseThen it queries the object for data associated with the Text format, with the autoConvert parameter set to false. 此试用版失败, 结果显示在标有 "Message #1" 的消息框中。This trial fails, and the result is displayed in a message box labeled "Message #1." 在第二次试验中, 它autoConvert将参数true设置为。In the second trial, it sets the autoConvert parameter to true. 此试用版成功, 结果显示在标有 "Message #2" 的消息框中。This trial succeeds, and the result is displayed in a message box labeled "Message #2." 该示例假设您已创建了一个FormForm1为的。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

注解

调用此方法可在调用DataObject GetData之前确定中是否存在格式。Call this method to determine whether a format exists in this DataObject before calling GetData. 调用GetFormats此实例中可用的格式。Call GetFormats for the formats that are available in this instance.

此方法在true以下情况下返回:This method returns true when:

  • autoConvert参数为true , 数据的格式可以转换为适当的格式。The autoConvert parameter is true and the data is in a format that can be converted to the appropriate format.

  • autoConvert参数为false , 数据采用适当的格式。The autoConvert parameter is false and the data is in the appropriate format.

此方法在false以下情况下返回:This method returns false when:

  • 参数为true , 并且此方法无法找到具有autoConvert false指定格式的数据, 并且它不能将数据转换为指定格式, 也不能将数据转换为指定的格式。 autoConvertThe 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.

  • 参数为, false并且此实例中不存在具有指定格式的数据。 autoConvertThe autoConvert parameter is false, and data does not exist in this instance in the specified format.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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. 例如, 以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

有关此方法的实现, 请参见DataObject.GetDataPresentFor an implementation of this method, see DataObject.GetDataPresent.

另请参阅

适用于