Bewerken

Share via


IDataObject.SetData Method

Definition

Stores the specified data and its associated format in this instance.

Overloads

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);
public void SetData (object? data);
abstract member SetData : obj -> unit
Public Sub SetData (data As Object)

Parameters

data
Object

The data to store.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 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. The 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

Remarks

The format is derived from the data class.

Data stored using this method can be converted to a compatible format when it is retrieved.

For an implementation of this method, see DataObject.SetData.

See also

Applies to

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);
public void SetData (string format, object? data);
abstract member SetData : string * obj -> unit
Public Sub SetData (format As String, data As Object)

Parameters

format
String

The format associated with the data. See DataFormats for predefined formats.

data
Object

The data to store.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. First, it creates a data object (myDataObject) and stores a string in the object specifying the UnicodeText format. 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. This 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

Remarks

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.

For an implementation of this method, see DataObject.SetData.

See also

Applies to

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);
public void SetData (Type format, object? data);
abstract member SetData : Type * obj -> unit
Public Sub SetData (format As Type, data As Object)

Parameters

format
Type

A Type representing the format associated with the data. See DataFormats for predefined formats.

data
Object

The data to store.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. 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. The 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

Remarks

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.

For an implementation of this method, see DataObject.SetData.

See also

Applies to

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);
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)

Parameters

format
String

The format associated with the data. See DataFormats for predefined formats.

autoConvert
Boolean

true to allow the data to be converted to another format; otherwise, false.

data
Object

The data to store.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the SetData method. First, 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. The only format associated with the data is the UnicodeText format. This 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

Remarks

If you do not know the format of the target application, you can store data in multiple formats using this method.

For an implementation of this method, see DataObject.SetData.

See also

Applies to