Sunday, August 29, 2010

Interesting Clipboard Functionality

Last week’s blog post was an excerpt from my .Net Tips column in the April 2008 Issue of the Universal Thread Magazine. Well, that was such a good issue for Tips, that I'm going to post another one from the same issue. I hope no one minds! ;0)

Did you ever need to copy an instantiated class to the clipboard, retreive it and access a property or method on it? Well, here's how to do it:

Clipboard.SetData("MyClassFormat", new MyClass("42"));
if (Clipboard.ContainsData("MyClassFormat"))
MyClass o = Clipboard.GetData("MyClassFormat") as MyClass;
if (o != null)
// do stuff here, such as execute a method
// o.MyMethod();
// or set a property
// o.MyProperty = "xyz";
MessageBox.Show("Ain't nothing there!!");

There's only one gotcha and that is that your class must have the [Serializable] attribute.

Thanks to Einar Kvandahl in Message #1135004 on the Universal Thread.

No comments:

Post a Comment