Export (0) Print
Expand All

DataObject Class

Implements a basic data transfer mechanism.

System.Object
  System.Windows.Forms.DataObject

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

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public Class DataObject _
	Implements IDataObject, IDataObject

The DataObject type exposes the following members.

  NameDescription
Public methodDataObjectInitializes a new instance of the DataObject class.
Public methodDataObject(Object)Initializes a new instance of the DataObject class and adds the specified object to it.
Public methodDataObject(String, Object)Initializes a new instance of the DataObject class and adds the specified object in the specified format.
Top

  NameDescription
Public methodContainsAudioIndicates whether the data object contains data in the WaveAudio format.
Public methodContainsFileDropListIndicates whether the data object contains data that is in the FileDrop format or can be converted to that format.
Public methodContainsImageIndicates whether the data object contains data that is in the Bitmap format or can be converted to that format.
Public methodContainsTextIndicates whether the data object contains data in the UnicodeText format.
Public methodContainsText(TextDataFormat)Indicates whether the data object contains text data in the format indicated by the specified TextDataFormat value.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAudioStreamRetrieves an audio stream from the data object.
Public methodGetData(String)Returns the data associated with the specified data format.
Public methodGetData(Type)Returns the data associated with the specified class type format.
Public methodGetData(String, Boolean)Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.
Public methodGetDataPresent(String)Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.
Public methodGetDataPresent(Type)Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.
Public methodGetDataPresent(String, Boolean)Determines whether this DataObject contains data in the specified format or, optionally, contains data that can be converted to the specified format.
Public methodGetFileDropListRetrieves a collection of file names from the data object.
Public methodGetFormatsReturns a list of all formats that data stored in this DataObject is associated with or can be converted to.
Public methodGetFormats(Boolean)Returns a list of all formats that data stored in this DataObject is associated with or can be converted to, using an automatic conversion parameter to determine whether to retrieve only native data formats or all formats that the data can be converted to.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetImageRetrieves an image from the data object.
Public methodGetTextRetrieves text data from the data object in the UnicodeText format.
Public methodGetText(TextDataFormat)Retrieves text data from the data object in the format indicated by the specified TextDataFormat value.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetAudio(Byte())Adds a Byte array to the data object in the WaveAudio format after converting it to a Stream.
Public methodSetAudio(Stream)Adds a Stream to the data object in the WaveAudio format.
Public methodSetData(Object)Adds the specified object to the DataObject using the object type as the data format.
Public methodSetData(String, Object)Adds the specified object to the DataObject using the specified format.
Public methodSetData(Type, Object)Adds the specified object to the DataObject using the specified type as the format.
Public methodSetData(String, Boolean, Object)Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.
Public methodSetFileDropListAdds a collection of file names to the data object in the FileDrop format.
Public methodSetImageAdds an Image to the data object in the Bitmap format.
Public methodSetText(String)Adds text data to the data object in the UnicodeText format.
Public methodSetText(String, TextDataFormat)Adds text data to the data object in the format indicated by the specified TextDataFormat value.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIDataObject.DAdviseInfrastructure. Creates a connection between a data object and an advisory sink. This method is called by an object that supports an advisory sink and enables the advisory sink to be notified of changes in the object's data.
Explicit interface implemetationPrivate methodIDataObject.DUnadviseInfrastructure. Destroys a notification connection that had been previously established.
Explicit interface implemetationPrivate methodIDataObject.EnumDAdviseInfrastructure. Creates an object that can be used to enumerate the current advisory connections.
Explicit interface implemetationPrivate methodIDataObject.EnumFormatEtcInfrastructure. Creates an object for enumerating the FORMATETC structures for a data object. These structures are used in calls to GetData or SetData.
Explicit interface implemetationPrivate methodIDataObject.GetCanonicalFormatEtcInfrastructure. Provides a standard FORMATETC structure that is logically equivalent to a more complex structure. Use this method to determine whether two different FORMATETC structures would return the same data, removing the need for duplicate rendering.
Explicit interface implemetationPrivate methodIDataObject.GetDataInfrastructure. Obtains data from a source data object. The GetData method, which is called by a data consumer, renders the data described in the specified FORMATETC structure and transfers it through the specified STGMEDIUM structure. The caller then assumes responsibility for releasing the STGMEDIUM structure.
Explicit interface implemetationPrivate methodIDataObject.GetDataHereInfrastructure. Obtains data from a source data object. This method, which is called by a data consumer, differs from the GetData method in that the caller must allocate and free the specified storage medium.
Explicit interface implemetationPrivate methodIDataObject.QueryGetDataInfrastructure. Determines whether the data object is capable of rendering the data described in the FORMATETC structure. Objects attempting a paste or drop operation can call this method before calling GetData to get an indication of whether the operation may be successful.
Explicit interface implemetationPrivate methodIDataObject.SetDataInfrastructure. Transfers data to the object that implements this method. This method is called by an object that contains a data source.
Top

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 a DataObject. Data is retrieved from a DataObject 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 a DataObject, pass the data to the constructor or call SetData. You can add data in multiple formats to the same DataObject. If you want the data you add to be retrieved in its native format only, call SetData(String, Boolean, Object) 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.

In .NET Framework 2.0, the DataObject class provides additional methods that make it easier to work with data in common formats. To add data of a particular format to the DataObject, use the appropriate SetFormat method, such as SetText. To retrieve data of a particular format from the DataObject, first call the appropriate ContainsFormat method (such as ContainsText) to determine whether the DataObject contains data in that format, then call the appropriate GetFormat method (such as GetText) to retrieve the data if the DataObject contains it.

NoteNote

Special considerations may be necessary when using the metafile format with the Clipboard. Due to a limitation in the current implementation of the DataObject class, the metafile format used by the .NET Framework may not be recognized by applications that use an older metafile format. In this case, you must interoperate with the Win32 Clipboard application programming interfaces (APIs). For more information, see article 323530, "Metafiles on Clipboard Are Not Visible to All Applications," in the Microsoft Knowledge Base at http://support.microsoft.com.

An object must be serializable for it to be put on the Clipboard. See System.Runtime.Serialization for more information on serialization. If your target application requires a very specific data format, the headers added to the data in the serialization process may prevent the application from recognizing your data. To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

The following code 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 requires that textBox1 has been created.

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

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 requires that textBox1 has been created.

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(DataFormats.Text).ToString()
End Sub 'GetMyData2

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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