次の方法で共有


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

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

Overloads Public Overridable Sub SetData( _
   ByVal format As Type, _   ByVal data As Object _) Implements IDataObject.SetData
[C#]
public virtual void SetData(Typeformat,objectdata);
[C++]
public: virtual void SetData(Type* format,Object* data);
[JScript]
public function SetData(
   format : Type,data : Object);

パラメータ

  • format
    データに関連付けられている形式を表す Type
  • data
    格納するデータ。

実装

IDataObject.SetData

解説

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

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

使用例

[Visual Basic, C#, C++] Type をデータ形式として使用して DataObject にデータを格納する例を次に示します。次に、 Type を使用してデータ形式を指定し、 GetData を呼び出してデータを取得します。結果はテキスト ボックスに表示されます。

[Visual Basic, C#, C++] このコードは、 textBox1 が作成されていることを前提にしています。

 
Private Sub AddMyData2()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Gets the type of the component.
    Dim myType As Type = myComponent.GetType()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub 'AddMyData2

[C#] 
private void AddMyData2() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Gets the type of the component.
    Type myType = myComponent.GetType();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
 

[C++] 
private:
void AddMyData2() {
    // Creates a component to store in the data object.
    Component* myComponent = new Component();
 
    // Gets the type of the component.
    Type* myType = myComponent->GetType();
 
    // Creates a new data object.
    DataObject* myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject->SetData(myType, myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    if(myDataObject->GetDataPresent(myType))
       textBox1->Text = String::Format( S"Data of type {0} is present in the DataObject", myType->Name );
    else
       textBox1->Text = String::Format( S"Data of type {0} is not present in the DataObject", myType->Name );
 }
 

[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 ファミリ

参照

DataObject クラス | DataObject メンバ | System.Windows.Forms 名前空間 | DataObject.SetData オーバーロードの一覧 | GetDataPresent | GetData | DataFormats | GetFormats | Type