Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataObject Class

Implements a basic data transfer mechanism.

For a list of all members of this type, see DataObject Members.

System.Object
   System.Windows.Forms.DataObject

[Visual Basic]
<ClassInterface(ClassInterfaceType.None)>
Public Class DataObject
   Implements IDataObject
[C#]
[ClassInterface(ClassInterfaceType.None)]
public class DataObject : IDataObject
[C++]
[ClassInterface(ClassInterfaceType::None)]
public __gc class DataObject : public IDataObject
[JScript]
public
   ClassInterface(ClassInterfaceType.None)
class DataObject implements IDataObject

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

DataObject implements the IDataObject interface, whose methods provide a format-independent mechanism for data transfer.

A DataObject is typically used with the Clipboard and in drag-and-drop operations. The DataObject class provides the recommended implementation of the IDataObject interface. It is suggested that you use the DataObject class rather than implementing IDataObject yourself.

Multiple pieces of data in different formats can be stored in an instance of DataObject. Data is retrieved from the instance by its associated format. Because the target application might not be known, you can increase the likelihood that the data will be in the appropriate format for an application by placing the data in a DataObject in multiple formats. See DataFormats for the predefined formats. You can implement your own format by creating an instance of the DataFormats.Format class.

To store data in an instance of this class, pass the data to the constructor or call SetData. You can add data in other formats to an instance of this class. If you want the data you add always to be retrieved in its native format, call SetData with the autoConvert parameter set to false.

Data can be retrieved from a DataObject in any format which is compatible with GetData. For example, text can be converted to Unicode. To retrieve data in the format in which it was stored, call GetData with the autoConvert parameter set to false.

To determine what formats the data is stored in, call GetFormats. To determine if a format is available, call GetDataPresent with the desired format.

Example

[Visual Basic, C#, C++] The following example adds data in a DataObject. First, a new DataObject is created and a component is stored in it. Then, it checks to see if data in the appropriate type exists in the DataObject. The result is displayed in a text box. This code assumes textBox1 has been created.

[Visual Basic] 
Private Sub AddMyData3()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    Dim myType As Type = myComponent.GetType()
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is not present in the DataObject"
    End If
End Sub 'AddMyData3


[C#] 
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.ToString() + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.ToString() +
       " is not present in the DataObject";
 }

[C++] 
private:
void AddMyData3() {
    // Creates a component to store in the data object.
    Component* myComponent = new Component();
 
    // Creates a new data object.
    DataObject* myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject->SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type* myType = myComponent->GetType();
    if(myDataObject->GetDataPresent(myType))
       textBox1->Text = String::Format( S"Data of type {0} is present in the DataObject", myType );
    else
       textBox1->Text = String::Format( S"Data of type {0} is not present in the DataObject", myType );
 }

[Visual Basic, C#, C++] The next example retrieves the data stored in a DataObject. First, a new DataObject is created with text data. Then, the data is retrieved, specifying its format as a string, and displayed in a text box. The data format is automatically converted from text to string. This code assumes textBox1 has been created.

[Visual Basic] 
Private Sub GetMyData2()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject(DataFormats.Text, "Text to Store")
    
    ' Prints the string in a text box.
    textBox1.Text = myDataObject.GetData("System.String").ToString()
End Sub 'GetMyData2

[C#] 
private void GetMyData2() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject(DataFormats.Text, "Text to Store");
 
    // Prints the string in a text box.
    textBox1.Text = myDataObject.GetData("System.String").ToString();
 }

[C++] 
private:
void GetMyData2() {
    // Creates a new data object using a string and the text format.
    DataObject* myDataObject = new DataObject(DataFormats::Text, S"Text to Store");
 
    // Prints the string in a text box.
    textBox1->Text = myDataObject->GetData(S"System.String")->ToString();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

DataObject Members | System.Windows.Forms Namespace | Clipboard | IDataObject | DataFormats

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.