A data object is represented by the NDataObject class. A data object is essentially a bag for binary data streams, each associated with a specific data format. A data object can contain only one data stream for a specific data format (i.e. you cannot have two or more data streams for the same data format - that is a one-to-one relationship).
A data object, when created by your code is by default writable (e.g. the IsReadOnly() method returns false), meaning that you can record data in various formats prior to publishing it via the NClipboard.SetDataObject or the NDragDrop.StartDragDrop methods. The following code creates a data object containing a simple Unicode text and places it in the clipboard:
Publishing a Data Object on the Clipboard |
Copy Code
|
---|---|
// publish a data object on the clipboard that contains a simple string NDataObject dataObject = new NDataObject(); // set data in the Unicode text format // NOTE: you can also set Unicode text by using the SetText shortcut method. dataObject.SetData(NDataFormat.TextFormat, "Hello world"); NClipboard.SetDataObject(dataObject); |
In the code sample above your code acts as a data object publisher.
A data object may be consumed by your code via calling the NClipbard.GetDataObject or by writing code that responds to Drag-And-Drop events (see User Input for more info). In these cases the data object is always readonly (e.g. the IsReadOnly() method returns true), thus preventing you from writing data to the data object (the SetData method for a readonly data object always throws an exception). The following code gets a data object from the clipboard and tries to obtain a Unicode text from it:
Consuming a Data Object from the Clipboard |
Copy Code
|
---|---|
// get a data object from the clipboard NDataObject dataObject = NClipboard.GetDataObject(); // query whether it contains data in the Unicode text format if (dataObject.ContainsData(NDataFormat.TextFormat)) { // try to obtain the Unicode text // NOTE: you can also get Unicode text by using the GetText shortcut method. string text = (string)dataObject.GetData(NDataFormat.TextFormat); if (text != null) { // successfully obtained Unicode text from the clipboard } } |
In the code sample above your code acts as a data object consumer. Besides querying for a specific format, you can get an array of all data formats in which a data object can render data via the GetFormats().