IDataObject.SetData 方法

定义

存储此实例中的指定数据及其关联格式。

重载

SetData(Object)

使用此格式的数据类存储此实例中的指定数据。

SetData(String, Object)

存储此实例中的指定数据及其关联格式。

SetData(Type, Object)

存储此实例中的指定数据及其关联类类型。

SetData(String, Boolean, Object)

在该实例中存储指定的数据及其关联格式,同时使用一个布尔值指定数据能否转换成另一格式。

SetData(Object)

使用此格式的数据类存储此实例中的指定数据。

public:
 void SetData(System::Object ^ data);
public void SetData (object data);
public void SetData (object? data);
abstract member SetData : obj -> unit
Public Sub SetData (data As Object)

参数

data
Object

要存储的数据。

示例

此示例使用 DataObject 实现 IDataObject的类来演示方法的使用 SetData 。 首先,它会创建组件 (myComponent) ,并将其存储在数据对象 (myDataObject) 中。 然后,它会检查指定的数据是否存储在数据对象中,并在消息框中显示结果。 该示例假定你已创建一个 Form 命名 Form1的 。

private:
   void SetData1()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the data object.
      myDataObject->SetData( myComponent );
      
      // Checks whether data of the specified type is in the data object.
      Type^ myType = myComponent->GetType();
      String^ myMessageText;
      if ( myDataObject->GetDataPresent( myType ) )
      {
         myMessageText = "Data of type " + myType->Name +
            " is present in the data object";
      }
      else
      {
         myMessageText = "Data of type " + myType->Name +
            " is not present in the data object";
      }
      
      // Displays the result in a message box.
      MessageBox::Show( myMessageText, "The Test Result" );
   }
private void SetData1() 
{
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a data object.
    DataObject myDataObject = new DataObject();

    // Adds the component to the data object.
    myDataObject.SetData(myComponent);
 
    // Checks whether data of the specified type is in the data object.
    Type myType = myComponent.GetType();
    string myMessageText;
    if(myDataObject.GetDataPresent(myType))
         myMessageText = "Data of type " + myType.Name + 
            " is present in the data object";
    else
        myMessageText = "Data of type " + myType.Name +
            " is not present in the data object";

    // Displays the result in a message box.
    MessageBox.Show(myMessageText, "The Test Result"); 
}
Private Sub SetData1()
   ' Creates a component to store in the data object.
   Dim myComponent As New System.ComponentModel.Component()
   
   ' Creates a data object.
   Dim myDataObject As New DataObject()
   
   ' Adds the component to the data object.
   myDataObject.SetData(myComponent)
   
   ' Checks whether data of the specified type is in the data object.
   Dim myType As Type = myComponent.GetType()
   Dim myMessageText As String
   If myDataObject.GetDataPresent(myType) Then
      myMessageText = "Data of type " + myType.Name + " is present in the data object"
   Else
      myMessageText = "Data of type " + myType.Name + " is not present in the data object"
   End If

   ' Displays the result in a message box.
   MessageBox.Show(myMessageText, "The Test Result")
End Sub

注解

格式派生自数据类。

检索时,可以使用此方法存储的数据转换为兼容格式。

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

另请参阅

适用于

SetData(String, Object)

存储此实例中的指定数据及其关联格式。

public:
 void SetData(System::String ^ format, System::Object ^ data);
public void SetData (string format, object data);
public void SetData (string format, object? data);
abstract member SetData : string * obj -> unit
Public Sub SetData (format As String, data As Object)

参数

format
String

与数据关联的格式。 请参见 DataFormats 以获取预定义的格式。

data
Object

要存储的数据。

示例

此示例使用 DataObject 实现 IDataObject的类来演示方法的使用 SetData 。 首先,它创建一个数据对象 (myDataObject) ,并将字符串存储在指定 UnicodeText 格式的对象中。 然后,它会检索存储在指定 Text 格式的对象中的数据,以便将数据转换为格式 Text 。 结果显示在消息框中。 此示例假定你已创建一个 Form 命名 Form1的 。

private:
   void SetData2()
   {
      // Creates a data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Stores a string, specifying the UnicodeText format.
      myDataObject->SetData( DataFormats::UnicodeText, "Hello World!" );
      
      // Retrieves the data by specifying the Text format.
      String^ myMessageText = "The data type is " +
         myDataObject->GetData( DataFormats::Text )->GetType()->Name;
      
      // Displays the result.
      MessageBox::Show( myMessageText, "The Test Result" );
   }
private void SetData2() 
{
    // Creates a data object.
    DataObject myDataObject = new DataObject();
 
    // Stores a string, specifying the UnicodeText format.
    myDataObject.SetData(DataFormats.UnicodeText, "Hello World!");
 
    // Retrieves the data by specifying the Text format.
    string myMessageText = "The data type is " + myDataObject.GetData(DataFormats.Text).GetType().Name;

    // Displays the result.
    MessageBox.Show(myMessageText, "The Test Result");
}
Private Sub SetData2()
   ' Creates a data object.
   Dim myDataObject As New DataObject()
   
   ' Stores a string, specifying the UnicodeText format.
   myDataObject.SetData(DataFormats.UnicodeText, "Hello World!")
   
   ' Retrieves the data by specifying the Text format.
   Dim myMessageText As String = "The data type is " & _
             myDataObject.GetData(DataFormats.Text).GetType().Name
   
   ' Displays the result.
   MessageBox.Show(myMessageText, "The Test Result")
End Sub

注解

如果不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。

检索时,可以使用此方法存储的数据转换为兼容格式。

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

另请参阅

适用于

SetData(Type, Object)

存储此实例中的指定数据及其关联类类型。

public:
 void SetData(Type ^ format, System::Object ^ data);
public void SetData (Type format, object data);
public void SetData (Type format, object? data);
abstract member SetData : Type * obj -> unit
Public Sub SetData (format As Type, data As Object)

参数

format
Type

表示与数据关联的格式的 Type。 请参见 DataFormats 以获取预定义的格式。

data
Object

要存储的数据。

示例

此示例使用 DataObject 实现 IDataObject的类来演示方法的使用 SetData 。 首先,它创建组件 (myComponent) ,并将其存储在数据对象 (myDataObject) 中,用于 myType 指定数据格式。 然后,它会检查指定类型的数据是否存储在对象中,并在消息框中显示结果。 该示例假定你已创建一个 Form 命名 Form1的 。

private:
   void SetData3()
   {
      // Creates a component.
      Component^ myComponent = gcnew Component;
      
      // Gets the type of the component.
      Type^ myType = myComponent->GetType();
      
      // Creates a data object.
      DataObject^ myDataObject = gcnew 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" );
   }
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");
}
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

注解

如果不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。

检索时,可以使用此方法存储的数据转换为兼容格式。

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

另请参阅

适用于

SetData(String, Boolean, Object)

在该实例中存储指定的数据及其关联格式,同时使用一个布尔值指定数据能否转换成另一格式。

public:
 void SetData(System::String ^ format, bool autoConvert, System::Object ^ data);
public void SetData (string format, bool autoConvert, object data);
public void SetData (string format, bool autoConvert, object? data);
abstract member SetData : string * bool * obj -> unit
Public Sub SetData (format As String, autoConvert As Boolean, data As Object)

参数

format
String

与数据关联的格式。 请参见 DataFormats 以获取预定义的格式。

autoConvert
Boolean

如果允许将数据转换成另一格式,值为 true;否则,值为 false

data
Object

要存储的数据。

示例

此示例使用 DataObject 实现 IDataObject的类来演示方法的使用 SetData 。 First, it creates a data object (myDataObject) and stores a UnicodeText string in it, with the autoConvert parameter set to false. 然后,它会检索与对象中存储的数据关联的格式 () ,并在消息框中显示结果。 与数据关联的唯一 UnicodeText 格式是格式。 此示例假定你已创建一个 Form 命名 Form1的 。

private:
   void SetData4()
   {
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;

      // Adds UnicodeText string to the object, and set the autoConvert
      // parameter to false.
      myDataObject->SetData( DataFormats::UnicodeText, false, "My text String*" );

      // Gets the data format(s) in the data object.
      array<String^>^arrayOfFormats = myDataObject->GetFormats();

      // Stores the results in a string.
      String^ theResult = "The format(s) associated with the data are: \n";
      for ( int i = 0; i < arrayOfFormats->Length; i++ )
         theResult = theResult + arrayOfFormats[ i ], " \n";

      // Show the results in a message box.
      MessageBox::Show( theResult );
   }
       private void SetData4() 
       {
           // Creates a new data object.
           DataObject myDataObject = new DataObject();

           // Adds UnicodeText string to the object, and set the autoConvert 
           // parameter to false.
           myDataObject.SetData(DataFormats.UnicodeText, false, "My text string");

           // Gets the data format(s) in the data object.
           String[] arrayOfFormats = myDataObject.GetFormats();

           // Stores the results in a string.
           string theResult = "The format(s) associated with the data are:" + '\n';
           for(int i=0; i<arrayOfFormats.Length; i++)
               theResult += arrayOfFormats[i] + '\n';
           
           // Show the results in a message box. 
           MessageBox.Show(theResult);
       }
Private Sub SetData4()
    ' Creates a new data object.
    Dim myDataObject As New DataObject()

    ' Adds UnicodeText string to the object, and set the autoConvert
    ' parameter to false.
    myDataObject.SetData(DataFormats.UnicodeText, False, "My text string")

    ' Gets the data format(s) in the data object.
    Dim arrayOfFormats As [String]() = myDataObject.GetFormats()

    ' Stores the results in a string.
    Dim theResult As String = "The format(s) associated with the data are:" + _
            ControlChars.Cr
    Dim i As Integer
    For i = 0 To arrayOfFormats.Length - 1
        theResult += arrayOfFormats(i) + ControlChars.Cr
    Next i
    ' Show the results in a message box. 
    MessageBox.Show(theResult)
End Sub

注解

如果不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。

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

另请参阅

适用于