Clipboard troubles

Hi!

I need to store/retrieve objects to/from the system clipboard. An abridged version of the code looks like this:

private void copyButton_Click(object sender,EventArgs e)
{
Clipboard.SetData(SHAPE_CLIPBOARD_FORMAT_NAME,Selected);
}

private void pasteButton_Click(object sender,EventArgs e)
{
if(Clipboard.ContainsData(SHAPE_CLIPBOARD_FORMAT_NAME))
{
BaseShape shape=(BaseShape)Clipboard.GetData(shapeFormat.Name);
if(shape!=null)
Shapes.Add(shape);
}
}

I get no exceptions, and the object in question is definitely serializable. Still, Clipboard.GetData() always returns null, even though ContainsDatat() returns true.

What's even more confusing: I used some freeware clipboard viewer to see what gets actually stored in the clipboard, and it showed nothing. Zero bytes of data. This obviously can't be right.

I'm now thinking of using an app-internal clipboard, and make it work with the system clipboard later, even though this would mean data cannot be transferred between different instances of my app for the time being.

[2556 byte] By [TimonChristl] at [2007-12-24]
# 1

Hi,

Could you please try this:

Replace the text with the u want to store in clipboard.
Clipboard.SetText(txtClipboard.Text);

To get the text which copied into clipboard and paste it into another textbox,
txtbox1.Text = Clipboard.GetText();

Thank you,
Bhanu.

BhanuPrakashNunna-MSFT at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
This must be a misunderstanding. I do not want to copy simple text to the clipboard, I want to copy arbitrarily complex objects. That's why I use Clipboard.SetData(), and that's why I talked about the object being serializable.
TimonChristl at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

You need to use the DataObject class to serialize your object. See the code pasted from MSDN below:

private void AddMyData3() {

// Creates a component to store in the data object.

Component myComponent = new Component();
// Creates a new data object.

DataObject myDataObject = new DataObject();
// Adds the component to the DataObject.

myDataObject.SetData(myComponent);
// Prints whether data of the specified type is in the DataObject.

Type myType = myComponent.GetType();

if(myDataObject.GetDataPresent(myType))

textBox1.Text = "Data of type " + myType.ToString() +

" is present in the DataObject";

else

textBox1.Text = "Data of type " + myType.ToString() +

" is not present in the DataObject";

}

bphillips76 at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Thank you all, but I've found the real reason now:

The class I was trying to serialize has events, the handlers for these are methods of a class that is not serializable by design). In my simple tests wether the object is indeed serializable I never attached any handlers to these events, so this didn't show up. And the final piece of this puzzle was that the Clipboard methods simply ignore any SerializationExceptions, which is a fundamentally bad design. Especially when the documentation for Clipboard.SetData() does not even mention this (Clipboard.SetDataObject() does, however).

After some searching, I found that I can write [field: NonSerializable] in front of the event declarations, which fixes the problem. It works now, I can use either SetData() or SetDataObject(), no problem.

TimonChristl at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...