IDataObject.SetData Método

Definição

Armazena os dados especificados e o seu formato associado neste caso.

Sobrecargas

Name Description
SetData(Object)

Armazena os dados especificados neste caso, usando a classe dos dados para o formato.

SetData(String, Object)

Armazena os dados especificados e o seu formato associado neste caso.

SetData(Type, Object)

Armazena os dados especificados e o seu tipo de classe associado neste caso.

SetData(String, Boolean, Object)

Armazena os dados especificados e o seu formato associado neste caso, usando um valor booleano para especificar se os dados podem ser convertidos para outro formato.

SetData(Object)

Armazena os dados especificados neste caso, usando a classe dos dados para o formato.

public:
 void SetData(System::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 armazenar.

Exemplos

Este exemplo utiliza a DataObject classe, que implementa IDataObject, para demonstrar o uso do SetData método. Primeiro, cria um componente (myComponent) e armazena-o num objeto de dados (myDataObject). Depois, verifica se os dados especificados estão armazenados no objeto de dados e apresenta o resultado numa caixa de mensagem. O exemplo assume que criou um 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

Observações

O formato é derivado da classe de dados.

Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

Para uma implementação deste método, veja DataObject.SetData.

Ver também

Aplica-se a

SetData(String, Object)

Armazena os dados especificados e o seu formato associado neste caso.

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)

Parâmetros

format
String

O formato associado aos dados. Veja DataFormats para formatos pré-definidos.

data
Object

Os dados a armazenar.

Exemplos

Este exemplo utiliza a DataObject classe, que implementa IDataObject, para demonstrar o uso do SetData método. Primeiro, cria um objeto de dados (myDataObject) e armazena uma string no objeto que especifica o UnicodeText formato. Depois, recupera esses dados armazenados no objeto especificando o Text formato, de modo a que os dados sejam convertidos para o Text formato. O resultado é apresentado numa caixa de mensagem. Este exemplo assume que criou um Form nome 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

Observações

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

Para uma implementação deste método, veja DataObject.SetData.

Ver também

Aplica-se a

SetData(Type, Object)

Armazena os dados especificados e o seu tipo de classe associado neste caso.

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)

Parâmetros

format
Type

A Type representa o formato associado aos dados. Veja DataFormats para formatos pré-definidos.

data
Object

Os dados a armazenar.

Exemplos

Este exemplo utiliza a DataObject classe, que implementa IDataObject, para demonstrar o uso do SetData método. Primeiro, cria um componente (myComponent) e armazena-o num objeto de dados (myDataObject), usando myType para especificar o formato dos dados. Depois, verifica se os dados do tipo especificado estão armazenados no objeto e apresenta o resultado numa caixa de mensagem. O exemplo assume que criou um 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

Observações

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

Para uma implementação deste método, veja DataObject.SetData.

Ver também

Aplica-se a

SetData(String, Boolean, Object)

Armazena os dados especificados e o seu formato associado neste caso, usando um valor booleano 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);
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 aos dados. Veja DataFormats para formatos pré-definidos.

autoConvert
Boolean

true para permitir a conversão dos dados para outro formato; caso contrário, false.

data
Object

Os dados a armazenar.

Exemplos

Este exemplo utiliza a DataObject classe, que implementa IDataObject, para demonstrar o uso do SetData método. Primeiro, cria um objeto de dados (myDataObject) e armazena uma UnicodeText string nele, com o autoConvert parâmetro definido para false. Depois, recupera o(s) formato(s) associado(s) aos dados armazenados no objeto e exibe o resultado numa caixa de mensagem. O único formato associado aos dados é o UnicodeText formato. Este exemplo assume que criou um Form nome 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

Observações

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Para uma implementação deste método, veja DataObject.SetData.

Ver também

Aplica-se a