次の方法で共有


IDataObject.SetData メソッド (Type, Object)

指定されているデータおよびそのデータに関連付けられているクラス型をインスタンスに格納します。

Sub SetData( _
   ByVal format As Type, _   ByVal data As Object _)
[C#]
void SetData(
   Typeformat,objectdata);
[C++]
void SetData(
   Type* format,Object* data);
[JScript]
function SetData(
   format : Type,data : Object);

パラメータ

  • format
    データに関連付けられている形式を表す Type 。定義済みの形式については、 DataFormats のトピックを参照してください。
  • data
    格納するデータ。

解説

データを取り出す側のアプリケーションの形式が不明な場合は、このメソッドを使用して、データを複数の形式で格納しておくことができます。

このメソッドを使用して格納されたデータは、取得時に互換性のある形式に変換できます。

このメソッドの実装については、 DataObject.SetData のトピックを参照してください。

使用例

[Visual Basic, C#, C++] この例では、 IDataObject を実装する DataObject クラスを使用して、 SetData メソッドの使用法を示します。最初に、コンポーネント (myComponent) を作成し、データ形式を指定する myType を使用して、データ オブジェクト (myDataObject) にそのコンポーネントを格納します。次に、指定した型のデータがそのデータ オブジェクトに格納されているかどうかを確認し、結果をメッセージ ボックスに表示します。この例は、 Form1 という名前の Form を作成済みであることを前提にしています。

 
Private Sub SetData3()
   ' Creates a component.
   Dim myComponent As New System.ComponentModel.Component()
   
   ' Gets the type of the component.
   Dim myType As Type = myComponent.GetType()
   
   ' Creates a data object.
   Dim myDataObject As New DataObject()
   
   ' Stores the component in the data object.
   myDataObject.SetData(myType, myComponent)
   
   ' Checks whether data of the specified type is in the data object.
   Dim myMessageText As String
   If myDataObject.GetDataPresent(myType) Then
      myMessageText = "Data of type " & myType.Name & " is stored in the data object"
   Else
      myMessageText = "No data of type " & myType.Name & " is stored in the data object"
   End If
   
   ' Displays the result.
   MessageBox.Show(myMessageText, "The Test Result")
End Sub 'SetData3

[C#] 
private void SetData3() 
{
    // Creates a component.
    Component myComponent = new Component();
 
    // Gets the type of the component.
    Type myType = myComponent.GetType();
 
    // Creates a data object.
    DataObject myDataObject = new DataObject();
 
    // Stores the component in the data object.
    myDataObject.SetData(myType, myComponent);
 
    // Checks whether data of the specified type is in the data object.
    string myMessageText;
    if(myDataObject.GetDataPresent(myType))
        myMessageText = "Data of type " + myType.Name + 
            " is stored in the data object";
    else
        myMessageText = "No data of type " + myType.Name +
            " is stored in the data object";
            
    // Displays the result.
    MessageBox.Show(myMessageText, "The Test Result");
}

[C++] 
private:
    void SetData3() {
        // Creates a component.
        Component* myComponent = new Component();

        // Gets the type of the component.
        Type*  myType = myComponent->GetType();

        // Creates a data object.
        DataObject* myDataObject = new DataObject();

        // Stores the component in the data object.
        myDataObject->SetData(myType, myComponent);

        // Checks whether data of the specified type is in the data object.
        String* myMessageText;
        if (myDataObject->GetDataPresent(myType))
            myMessageText = String::Concat(S"Data of type ", myType->Name,
                S" is stored in the data object");
        else
            myMessageText = String::Concat(S"No data of type ", myType->Name,
                S" is stored in the data object");

        // Displays the result.
        MessageBox::Show(myMessageText, S"The Test Result");
    }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

IDataObject インターフェイス | IDataObject メンバ | System.Windows.Forms 名前空間 | IDataObject.SetData オーバーロードの一覧 | IDataObject.GetData | IDataObject.GetDataPresent | DataObject.GetFormats | DataFormats