共用方式為


How to: Create a Data Object

The following examples show various ways to create a data object using the constructors provided by the DataObject class.

Example

The following example code creates a new data object and uses one of the overloaded constructors (DataObject) to initialize the data object with a string. In this case, an appropriate data format is determined automatically according to the stored data's type, and auto-converting of the stored data is allowed by default.

string stringData = "Some string data to store...";
DataObject dataObject = new DataObject(stringData);

The following example code is a condensed version of the code shown above.

DataObject dataObject = new DataObject("Some string data to store...");

Example

The following example code creates a new data object and uses one of the overloaded constructors (DataObject) to initialize the data object with a string and a specified data format. In this case the data format is specified by a string; the DataFormats class provides a set of pre-defined type strings. Auto-converting of the stored data is allowed by default.

string stringData = "Some string data to store...";
string dataFormat = DataFormats.UnicodeText;
DataObject dataObject = new DataObject(dataFormat, stringData);

The following example code is a condensed version of the code shown above.

DataObject dataObject = new DataObject(DataFormats.UnicodeText, "Some string data to store...");

Example

The following example code creates a new data object and uses one of the overloaded constructors (DataObject) to initialize the data object with a string and a specified data format. In this case the data format is specified by a Type parameter. Auto-converting of the stored data is allowed by default.

string stringData = "Some string data to store...";
Type dataFormat = stringData.GetType();
DataObject dataObject = new DataObject(dataFormat, stringData);

The following example code is a condensed version of the code shown above.

DataObject dataObject = new DataObject("".GetType(), "Some string data to store...");

Example

The following example code creates a new data object and uses one of the overloaded constructors (DataObject) to initialize the data object with a string and a specified data format. In this case the data format is specified by a string; the DataFormats class provides a set of pre-defined type strings. This particular constructor overload enables the caller to specify whether auto-converting is allowed.

string stringData = "Some string data to store...";
string dataFormat = DataFormats.Text;
bool autoConvert = false;
DataObject dataObject = new DataObject(dataFormat, stringData, autoConvert);

The following example code is a condensed version of the code shown above.

DataObject dataObject = new DataObject(DataFormats.Text, "Some string data to store...", false);

See Also

Reference

IDataObject

Other Resources

Drag and Drop Samples