Share via


IDataObject.SetData Método

Definição

Armazena os dados especificados e seu formato associado nesta instância.

Sobrecargas

SetData(Object)

Armazena os dados especificados nesta instância, usando a classe dos dados para o formato.

SetData(String, Object)

Armazena os dados especificados e seu formato associado nesta instância.

SetData(Type, Object)

Armazena os dados especificados e seu tipo de classe associado nesta instância.

SetData(String, Boolean, Object)

Armazena os dados especificados e seu formato associado nesta instância, usando um valor booliano para especificar se os dados podem ser convertidos para outro formato.

SetData(Object)

Armazena os dados especificados nesta instância, usando a classe dos dados para o formato.

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)

Parâmetros

data
Object

Os dados a serem armazenados.

Exemplos

Este exemplo usa a DataObject classe , que implementa IDataObject, para demonstrar o uso do SetData método . Primeiro, ele cria um componente (myComponent) e o armazena em um objeto de dados (myDataObject). Em seguida, ele verifica se os dados especificados são armazenados no objeto de dados e exibe o resultado em uma caixa de mensagem. O exemplo pressupõe que você tenha criado um Form chamado 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

Comentários

O formato é derivado da classe de dados .

Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando são recuperados.

Para obter uma implementação desse método, consulte DataObject.SetData.

Confira também

Aplica-se a

SetData(String, Object)

Armazena os dados especificados e seu formato associado nesta instância.

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)

Parâmetros

format
String

O formato associado a esses dados. Consulte DataFormats para obter os formatos predefinidos.

data
Object

Os dados a serem armazenados.

Exemplos

Este exemplo usa a DataObject classe , que implementa IDataObject, para demonstrar o uso do SetData método . Primeiro, ele cria um objeto de dados (myDataObject) e armazena uma cadeia de caracteres no objeto que especifica o UnicodeText formato. Em seguida, ele recupera os dados armazenados no objeto que especifica o Text formato, para que os dados sejam convertidos no Text formato . O resultado é exibido em uma caixa de mensagem. Este exemplo pressupõe que você tenha criado um Form chamado 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

Comentários

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando são recuperados.

Para obter uma implementação desse método, consulte DataObject.SetData.

Confira também

Aplica-se a

SetData(Type, Object)

Armazena os dados especificados e seu tipo de classe associado nesta instância.

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)

Parâmetros

format
Type

Um Type que representa o formato associado aos dados. Consulte DataFormats para obter os formatos predefinidos.

data
Object

Os dados a serem armazenados.

Exemplos

Este exemplo usa a DataObject classe , que implementa IDataObject, para demonstrar o uso do SetData método . Primeiro, ele cria um componente (myComponent) e o armazena em um objeto de dados (myDataObject), usando myType para especificar o formato de dados. Em seguida, ele verifica se os dados do tipo especificado estão armazenados no objeto e exibe o resultado em uma caixa de mensagem. O exemplo pressupõe que você tenha criado um Form chamado 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

Comentários

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando são recuperados.

Para obter uma implementação desse método, consulte DataObject.SetData.

Confira também

Aplica-se a

SetData(String, Boolean, Object)

Armazena os dados especificados e seu formato associado nesta instância, usando um valor booliano para especificar se os dados podem ser convertidos para outro formato.

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)

Parâmetros

format
String

O formato associado a esses dados. Consulte DataFormats para obter os formatos predefinidos.

autoConvert
Boolean

true para permitir que os dados sejam convertidos em outro formato; caso contrário, false.

data
Object

Os dados a serem armazenados.

Exemplos

Este exemplo usa a DataObject classe , que implementa IDataObject, para demonstrar o uso do SetData método . Primeiro, ele cria um objeto de dados (myDataObject) e armazena uma UnicodeText cadeia de caracteres nele, com o autoConvert parâmetro definido falsecomo . Em seguida, ele recupera os formatos associados aos dados armazenados no objeto e exibe o resultado em uma caixa de mensagem. O único formato associado aos dados é o UnicodeText formato . Este exemplo pressupõe que você tenha criado um Form chamado 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

Comentários

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Para obter uma implementação desse método, consulte DataObject.SetData.

Confira também

Aplica-se a