Clipboard - Dual Format - Text & Object

K

kevin

How do you allow an object to be stored on the clipboard such that it
still pastes in text format, but also can be pasted within the
application that is aware of how to deal with the object?

For example, I have this object I've called CompositeTextObject. An
instance of the object (cto) can have three properties:
prefix = "pre";
body = "123";
suffix = "suf";

Represented as ordinary text it would be "pre-123-suf".

I've been able to implement the cut/copy/paste of the
CompositeTextObject fine by using Clipboard.SetDataObject etc.

I've also tried to implement my own custom DataObject that implements
the IDataObject interface.

However, this doesn't seem to work.

Does somebody have working sample code that addresses this?

Thanks,
Kevin.
 
C

ClayB [Syncfusion]

I think your object has to be serializable so it can be placed on the
clipboard by the framework. So, you can either implement the iSerializable
interface or (depending on your object) mark it with the Serializable
attribute. Below are 3 button handlers that copy, and pasteastext and
pasteasobject, and a MyObject class that is serializable.


private void Copy_Click(object sender, System.EventArgs e)
{
MyObject someObject = new MyObject("hello", "there");
DataObject data = new DataObject();

//put text in the data object
data.SetData(someObject.sValue + "-" + someObject.tValue);

//put MyObject in the data object
data.SetData(typeof(MyObject), someObject);

//set the data object in the clipboard
Clipboard.SetDataObject(data);
}

private void PasteAsText_Click(object sender, System.EventArgs e)
{
//get text from clipboard
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
string text = (string)
Clipboard.GetDataObject().GetData(DataFormats.Text);
Console.WriteLine("Got text: " + text);
}
}

private void PasteAsMyObject_Click(object sender, System.EventArgs e)
{
//get as object
if(Clipboard.GetDataObject().GetDataPresent(typeof(MyObject)))
{
MyObject obj = Clipboard.GetDataObject().GetData(typeof(MyObject))as
MyObject;
if(obj != null)
{
Console.WriteLine("Got MyObject: " + obj.sValue + "_" + obj.tValue);
}
}
}

[Serializable]
public class MyObject
{
public MyObject(string s, string t)
{
this.s = s;
this.t = t;
}

private string s;
public string sValue
{
get{return s;}
set{s = value;}
}

private string t;
public string tValue
{
get{return t;}
set{t = value;}
}
}

===============================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
K

kevin

Thanks, this works great!

I wasn't aware that you could call SetData twice with two different data
types. I was trying to do it by creating my own MyDataObject that
implemented IDataObject. And in MyDataObject I had implemented
GetDataPresent so that it returned true when queried for text or object
data. Ditto for GetData - it converted to text by calling ToString().
Of course, this didn't work for some reason! BTW, I did have the object
defined with the Serializable attribute.

Thanks,
Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top