IDataObject.GetDataPresent 方法

定义

确定此实例中存储的数据是否与指定的格式关联。

重载

GetDataPresent(String)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。

GetDataPresent(Type)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。

GetDataPresent(String, Boolean)

确定该实例中存储的数据是否与指定格式关联,同时使用一个布尔值确定是否将数据转换成此格式。

GetDataPresent(String)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。

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

要检查的格式。 请参见 DataFormats 以获取预定义的格式。

返回

如果此实例中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 false

示例

此示例使用 DataObject 实现 的 IDataObject类来演示 方法的使用 GetDataPresent 。 首先,它使用字符串和 Text 格式创建数据对象。 然后,它会验证数据是否以 格式 Text 存在,并在消息框中显示结果。 该示例假定你已创建名为 FormForm1

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之前此中是否存在格式。 为此实例中可用的格式调用 GetFormats

注意

如果存储数据时指定允许转换,并且请求的格式与存储的格式兼容,则可以将数据转换为另一种格式。 例如,存储为 Unicode 的数据可以转换为文本。

有关此方法的实现,请参阅 DataObject.GetDataPresent

另请参阅

适用于

GetDataPresent(Type)

确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。

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 表示要检查的格式。 请参见 DataFormats 以获取预定义的格式。

返回

如果此实例中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 false

示例

此示例使用 DataObject 实现 的 IDataObject类来演示 方法的使用 GetDataPresent 。 首先,它创建一个组件 (myComponent) ,并将其存储在数据对象 (myDataObject) 中。 然后,它会检查指定的数据是否存储在 中 myDataObject。 如果测试的计算结果 true为 ,它将在消息框中显示结果,并在文本框中显示数据类型。 此示例假定你已创建了一个名为 FormForm1 和一个名为 TextBoxtextBox1

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之前此中是否存在格式。 为此实例中可用的格式调用 GetFormats

注意

如果存储数据时指定允许转换,并且请求的格式与存储的格式兼容,则可以将数据转换为另一种格式。 例如,存储为 Unicode 的数据可以转换为文本。

有关此方法的实现,请参阅 DataObject.GetDataPresent

另请参阅

适用于

GetDataPresent(String, Boolean)

确定该实例中存储的数据是否与指定格式关联,同时使用一个布尔值确定是否将数据转换成此格式。

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

要检查的格式。 请参见 DataFormats 以获取预定义的格式。

autoConvert
Boolean

值为 true 时,确定此实例中存储的数据是否可以转换成指定的格式;值为 false 时,检查数据的格式是否为指定的格式。

返回

如果数据采用指定的格式,或者可以转换成指定的格式,则为 true;否则为 false

示例

此示例使用 DataObject 实现 的 IDataObject类来演示 方法的使用 GetDataPresent 。 首先,它使用字符串和 Text 格式创建 (myDataObject) 的数据对象。 然后,它会在 对象中查询与 Text 格式关联的数据,并将 autoConvert 参数设置为 false。 此试用版失败,结果显示在标有“消息 #1”的消息框中。第二次试用中,它将 参数设置为 autoConverttrue。 此试用成功,结果显示在标有“消息 #2”的消息框中。该示例假定你已创建名为 FormForm1

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之前此中是否存在格式。 为此实例中可用的格式调用 GetFormats

此方法在 true 以下情况下返回:

  • autoConvert参数为 true ,数据采用可转换为适当格式的格式。

  • 参数 autoConvertfalse ,数据采用适当的格式。

此方法在 false 以下情况下返回:

  • autoConvert参数为 true ,此方法找不到指定格式的数据,并且无法将数据转换为指定格式,或者数据存储时autoConvert设置为 false

  • 参数 autoConvertfalse,并且此实例中不存在指定格式的数据。

注意

如果存储数据时指定允许转换,并且请求的格式与存储的格式兼容,则可以将数据转换为另一种格式。 例如,存储为 Unicode 的数据可以转换为文本。

有关此方法的实现,请参阅 DataObject.GetDataPresent

另请参阅

适用于