System.Windows.Forms.DataObject Class

Implements a basic data transfer mechanism.

See Also: DataObject Members

Syntax

[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class DataObject : System.Runtime.InteropServices.ComTypes.IDataObject, IDataObject

Remarks

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

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

Multiple pieces of data in different formats can be stored in a System.Windows.Forms.DataObject. Data is retrieved from a System.Windows.Forms.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 System.Windows.Forms.DataObject in multiple formats. See System.Windows.Forms.DataFormats for the predefined formats. You can implement your own format by creating an instance of the System.Windows.Forms.DataFormats.Format class.

To store data in a System.Windows.Forms.DataObject, pass the data to the constructor or call erload:System.Windows.Forms.DataObject.SetData. You can add data in multiple formats to the same System.Windows.Forms.DataObject. If you want the data you add to be retrieved in its native format only, call DataObject.SetData(string, bool, object) with the autoConvert parameter set to false.

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

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

In dnprdnext, the System.Windows.Forms.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 System.Windows.Forms.DataObject, use the appropriate SetFormat method, such as erload:System.Windows.Forms.DataObject.SetText. To retrieve data of a particular format from the System.Windows.Forms.DataObject, first call the appropriate ContainsFormat method (such as erload:System.Windows.Forms.DataObject.ContainsText) to determine whether the System.Windows.Forms.DataObject contains data in that format, then call the appropriate GetFormat method (such as erload:System.Windows.Forms.DataObject.GetText) to retrieve the data if the System.Windows.Forms.DataObject contains it.

Note:

Special considerations may be necessary when using the metafile format with the Clipboard. Due to a limitation in the current implementation of the System.Windows.Forms.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 System.IO.MemoryStream and pass the System.IO.MemoryStream to the DataObject.SetData(object) method.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0