IDataObject.SetData 方法

定义

存储此实例中的指定数据及其关联格式。Stores the specified data and its associated format in this instance.

重载

SetData(Object)

使用此格式的数据类存储此实例中的指定数据。Stores the specified data in this instance, using the class of the data for the format.

SetData(String, Object)

存储此实例中的指定数据及其关联格式。Stores the specified data and its associated format in this instance.

SetData(Type, Object)

存储此实例中的指定数据及其关联类类型。Stores the specified data and its associated class type in this instance.

SetData(String, Boolean, Object)

在该实例中存储指定的数据及其关联格式,同时使用一个布尔值指定数据能否转换成另一格式。Stores the specified data and its associated format in this instance, using a Boolean value to specify whether the data can be converted to another format.

SetData(Object)

使用此格式的数据类存储此实例中的指定数据。Stores the specified data in this instance, using the class of the data for the format.

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

参数

data
Object

要存储的数据。The data to store.

示例

此示例使用 DataObject 实现的类 IDataObject 来演示方法的用法 SetDataThis example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 首先,它将创建一个组件 (myComponent) 并将其存储在数据对象 (myDataObject) 。First, it creates a component (myComponent) and stores it in a data object (myDataObject). 然后,它会检查指定的数据是否存储在数据对象中,并将结果显示在一个消息框中。Then it checks whether the specified data is stored in the data object, and displays the result in a message box. 该示例假设您已创建了一个 Form 名为的 Form1The example assumes that you have created a Form named 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

注解

格式是从数据类派生的。The format is derived from the data class.

在检索使用此方法存储的数据时,可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

有关此方法的实现,请参见 DataObject.SetDataFor an implementation of this method, see DataObject.SetData.

另请参阅

适用于

SetData(String, Object)

存储此实例中的指定数据及其关联格式。Stores the specified data and its associated format in this instance.

public:
 void SetData(System::String ^ format, System::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

与数据关联的格式。The format associated with the data. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

data
Object

要存储的数据。The data to store.

示例

此示例使用 DataObject 实现的类 IDataObject 来演示方法的用法 SetDataThis example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 首先,它创建一个 () 的数据对象 myDataObject ,并将一个字符串存储在指定该格式的对象中 UnicodeTextFirst, it creates a data object (myDataObject) and stores a string in the object specifying the UnicodeText format. 然后,它检索存储在对象中的数据以指定 Text 格式,以便将数据转换为 Text 格式。Then it retrieves that data stored in the object specifying the Text format, so that the data is converted to the Text format. 结果显示在一个消息框中。The result is displayed in a message box. 此示例假设您已创建了一个 Form 名为的 Form1This example assumes that you have created a Form named 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

注解

如果您不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

在检索使用此方法存储的数据时,可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

有关此方法的实现,请参见 DataObject.SetDataFor an implementation of this method, see DataObject.SetData.

另请参阅

适用于

SetData(Type, Object)

存储此实例中的指定数据及其关联类类型。Stores the specified data and its associated class type in this instance.

public:
 void SetData(Type ^ format, System::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

表示与数据关联的格式的 TypeA Type representing the format associated with the data. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

data
Object

要存储的数据。The data to store.

示例

此示例使用 DataObject 实现的类 IDataObject 来演示方法的用法 SetDataThis example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 首先,它将创建一个组件 (myComponent) 并将其存储在数据对象 (myDataObject) 中,并使用 myType 指定数据格式。First, it creates a component (myComponent) and stores it in a data object (myDataObject), using myType to specify the data format. 然后,它会检查指定类型的数据是否存储在对象中,并将结果显示在一个消息框中。Then it checks whether the data of the specified type is stored in the object, and displays the result in a message box. 该示例假设您已创建了一个 Form 名为的 Form1The example assumes that you have created a Form named 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

注解

如果您不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

在检索使用此方法存储的数据时,可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

有关此方法的实现,请参见 DataObject.SetDataFor an implementation of this method, see DataObject.SetData.

另请参阅

适用于

SetData(String, Boolean, Object)

在该实例中存储指定的数据及其关联格式,同时使用一个布尔值指定数据能否转换成另一格式。Stores the specified data and its associated format in this instance, using a Boolean value to specify whether the data can be converted to another format.

public:
 void SetData(System::String ^ format, bool autoConvert, System::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

与数据关联的格式。The format associated with the data. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean

如果允许将数据转换成另一格式,值为 true;否则,值为 falsetrue to allow the data to be converted to another format; otherwise, false.

data
Object

要存储的数据。The data to store.

示例

此示例使用 DataObject 实现的类 IDataObject 来演示方法的用法 SetDataThis example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 首先,它创建一个 () 的数据对象, myDataObjectUnicodeText 在其中存储一个字符串,并将 autoConvert 参数设置为 falseFirst, it creates a data object (myDataObject) and stores a UnicodeText string in it, with the autoConvert parameter set to false. 然后,它检索与存储在对象中的数据关联的格式 () ,并将结果显示在一个消息框中。Then it retrieves the format(s) associated with the data stored in the object and displays the result in a message box. 与数据关联的唯一格式是 UnicodeText 格式。The only format associated with the data is the UnicodeText format. 此示例假设您已创建了一个 Form 名为的 Form1This example assumes that you have created a Form named 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

注解

如果您不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

有关此方法的实现,请参见 DataObject.SetDataFor an implementation of this method, see DataObject.SetData.

另请参阅

适用于