You publish data on the clipboard via the NClipboard.SetDataObject(NDataObject dataObject) method. It sets the specified data object on the clipboard. If null is passed the clipboard is cleared (placing an empty data object on the clipboard has the same effect). After the call the specified data object automatically becomes readonly. The following code sample publishes a text string:
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 dataObject.SetData(NDataFormat.TextFormat, "Hello world"); NClipboard.SetDataObject(dataObject); |
You consume data from the clipboard via the NDataObject NClipboard.GetDataObject() method. Its gets the current data object from the clipboard. If the clipboard clipboard is empty it will return an empty data object (i.e. will never return a null data object). The returned data object is always readonly.
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 } } |
Because in both the consumer and publisher roles the clipboard operations are only dealing with a single data object, the NClipboard static class provides helper methods similar to the helper methods of a NDataObject, that help you work more easily with the predefined formats. Following is a brief summary: