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

傳回

Boolean

如果儲存在這個執行個體中的資料與指定的格式相關,或是可以轉換成指定的格式則為 true,否則為 false

範例

這個範例會 DataObject 使用 實作 IDataObject 的 類別來示範 方法的使用 GetDataPresent 方式。 首先,它會使用字串和 Text 格式來建立資料物件。 然後它會驗證資料是否以格式顯示 Text ,並在訊息方塊中顯示結果。 此範例假設您已建立名為 的 Form 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

備註

呼叫這個方法,在呼叫 GetData 之前,先判斷格式是否存在於這個 DataObject 中。 呼叫 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

傳回

Boolean

如果儲存在這個執行個體中的資料與指定的格式相關,或是可以轉換成指定的格式則為 true,否則為 false

範例

這個範例會 DataObject 使用 實作 IDataObject 的 類別來示範 方法的使用 GetDataPresent 方式。 首先,它會建立元件 (myComponent) ,並將它儲存在資料物件 (myDataObject) 中。 然後它會檢查指定的資料是否儲存在 中 myDataObject 。 如果測試評估 true ,它會在訊息方塊中顯示結果,並在文字方塊中顯示資料類型。 此範例假設您已經建立 Form 名為 的 ,以及名為 的 textBox1 TextBoxForm1

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

備註

呼叫這個方法,在呼叫 GetData 之前,先判斷格式是否存在於這個 DataObject 中。 呼叫 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

傳回

Boolean

如果資料在指定的格式中,或是可以轉換成指定的格式,則為 true;否則為 false

範例

這個範例會使用 DataObject 實作 IDataObject 的 類別來示範 方法的使用 GetDataPresent 方式。 首先,它會使用字串和 Text 格式建立資料物件 (myDataObject) 。 然後它會查詢 物件,以取得與格式相關聯的 Text 資料,並將 autoConvert 參數設定為 false 。 此試用版失敗,結果會顯示在標示為「訊息 #1」的訊息方塊中。 在第二個試用版中,它會將 autoConvert 參數設定為 true 。 此試用版成功,結果會顯示在標示為「訊息 #2」的訊息方塊中。 此範例假設您已建立 Form 名為 的 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

備註

呼叫這個方法,在呼叫 GetData 之前,先判斷格式是否存在於這個 DataObject 中。 呼叫 GetFormats 這個實例中可用的格式。

這個方法會在下列情況下傳 true 回:

  • 參數 autoConverttrue ,而且資料的格式可以轉換成適當的格式。

  • 參數 autoConvertfalse ,且資料的格式適當。

這個方法會在下列情況下傳 false 回:

  • 參數 autoConverttrue ,且這個方法無法尋找指定格式的資料,而且無法將資料轉換成指定的格式,或將資料儲存 autoConvert false 為 。

  • 參數 autoConvertfalse ,而且資料不存在於這個實例中,格式為指定的。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

如需此方法的實作,請參閱 DataObject.GetDataPresent

另請參閱

適用於