Putting custom objects on the clipboard

K

Keith

All,

It seems that there has been some discussion of this issue, but I can't find
any real answers. How does one copy a custom object onto the clipboard and
then retrieve it? I can do this with standard types and collections of
standard types, but if I use a custom object the retrieved objects are
always null. For example, given the following class:

public class MyObject : Object

{

private string m_Name = "";

public MyObject(string name)

{

m_Name = name;

}

public string Name { get { return "MyObject:"+m_Name; } }

}

this code does not successfully copy/retrieve a MyObject object usign the
Clipboard:
MyObject myObj = new MyObject("Foo");

DataFormats.Format f = DataFormats.GetFormat("MyObject");

DataObject dObj = new DataObject(f.Name, myObj);

Clipboard.SetDataObject(dObj, false);

// Get it right back out

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(f.Name))

{

MyObject stuff = (MyObject)obj.GetData(f.Name, true);

if(stuff == null)

MessageBox.Show("GetData failed for " + f.Name + "!");

else

{

MessageBox.Show("MyObject on Clipboard was: " + stuff.Name,"GetData
successful!");

}

}

The GetDataPresent call is successful, but a null reference always comes
back from GetData; the autoConvert param doesn't seem to have any affect.
Do custom objects have to implement some interface or derive from some base
clsss to make this possible? Must they be cloneable? Serializable?

Any advice or suggestions?

Keith
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Keith,

The type of the object that you put in the clipboard has to be serializable.
Add the [Serializable] attrinute above the class declaration and the sample
will work.
 
K

Keith

Thanks for the reply. I managed to find this out in a VB.NET book, and
managed to figure out some other things too, like is the class is derived,
all classes in the derivation tree must use [Serializable].

However, I'm still somewhat stuck.

The actual objects that I wanted to put on the clipboard contain all sorts
of complex things like hashtables, weak references and other complex
objects. Is it even possible to do this, or do I need to figure out some
sort of "lightweight" way (like an intermediate object) to transfer
essential data onto the clipboard and back?

Keith

Stoitcho Goutsev (100) said:
Keith,

The type of the object that you put in the clipboard has to be
serializable. Add the [Serializable] attrinute above the class declaration
and the sample will work.


--
Stoitcho Goutsev (100) [C# MVP]

Keith said:
All,

It seems that there has been some discussion of this issue, but I can't
find any real answers. How does one copy a custom object onto the
clipboard and then retrieve it? I can do this with standard types and
collections of standard types, but if I use a custom object the retrieved
objects are always null. For example, given the following class:

public class MyObject : Object

{

private string m_Name = "";

public MyObject(string name)

{

m_Name = name;

}

public string Name { get { return "MyObject:"+m_Name; } }

}

this code does not successfully copy/retrieve a MyObject object usign the
Clipboard:
MyObject myObj = new MyObject("Foo");

DataFormats.Format f = DataFormats.GetFormat("MyObject");

DataObject dObj = new DataObject(f.Name, myObj);

Clipboard.SetDataObject(dObj, false);

// Get it right back out

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(f.Name))

{

MyObject stuff = (MyObject)obj.GetData(f.Name, true);

if(stuff == null)

MessageBox.Show("GetData failed for " + f.Name + "!");

else

{

MessageBox.Show("MyObject on Clipboard was: " + stuff.Name,"GetData
successful!");

}

}

The GetDataPresent call is successful, but a null reference always comes
back from GetData; the autoConvert param doesn't seem to have any affect.
Do custom objects have to implement some interface or derive from some
base clsss to make this possible? Must they be cloneable? Serializable?

Any advice or suggestions?

Keith
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Keith,

If the data is so comlplex and contain objects of types not marked as
'Serializable' I'd suggest that you make your class custom sarializable via
implementing ISerializable interface.

For more info read

Part I
Keith said:
Thanks for the reply. I managed to find this out in a VB.NET book, and
managed to figure out some other things too, like is the class is derived,
all classes in the derivation tree must use [Serializable].

However, I'm still somewhat stuck.

The actual objects that I wanted to put on the clipboard contain all sorts
of complex things like hashtables, weak references and other complex
objects. Is it even possible to do this, or do I need to figure out some
sort of "lightweight" way (like an intermediate object) to transfer
essential data onto the clipboard and back?

Keith

Stoitcho Goutsev (100) said:
Keith,

The type of the object that you put in the clipboard has to be
serializable. Add the [Serializable] attrinute above the class
declaration and the sample will work.


--
Stoitcho Goutsev (100) [C# MVP]

Keith said:
All,

It seems that there has been some discussion of this issue, but I can't
find any real answers. How does one copy a custom object onto the
clipboard and then retrieve it? I can do this with standard types and
collections of standard types, but if I use a custom object the
retrieved objects are always null. For example, given the following
class:

public class MyObject : Object

{

private string m_Name = "";

public MyObject(string name)

{

m_Name = name;

}

public string Name { get { return "MyObject:"+m_Name; } }

}

this code does not successfully copy/retrieve a MyObject object usign
the Clipboard:
MyObject myObj = new MyObject("Foo");

DataFormats.Format f = DataFormats.GetFormat("MyObject");

DataObject dObj = new DataObject(f.Name, myObj);

Clipboard.SetDataObject(dObj, false);

// Get it right back out

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(f.Name))

{

MyObject stuff = (MyObject)obj.GetData(f.Name, true);

if(stuff == null)

MessageBox.Show("GetData failed for " + f.Name + "!");

else

{

MessageBox.Show("MyObject on Clipboard was: " + stuff.Name,"GetData
successful!");

}

}

The GetDataPresent call is successful, but a null reference always comes
back from GetData; the autoConvert param doesn't seem to have any
affect. Do custom objects have to implement some interface or derive
from some base clsss to make this possible? Must they be cloneable?
Serializable?

Any advice or suggestions?

Keith
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Sorry
Part I
http://msdn.microsoft.com/msdnmag/issues/02/04/net/default.aspx
Part II
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
Part III
http://msdn.microsoft.com/msdnmag/issues/02/09/net/


HTH
Stoitcho Goutsev (100) [C# MVP]

Keith said:
Thanks for the reply. I managed to find this out in a VB.NET book, and
managed to figure out some other things too, like is the class is derived,
all classes in the derivation tree must use [Serializable].

However, I'm still somewhat stuck.

The actual objects that I wanted to put on the clipboard contain all sorts
of complex things like hashtables, weak references and other complex
objects. Is it even possible to do this, or do I need to figure out some
sort of "lightweight" way (like an intermediate object) to transfer
essential data onto the clipboard and back?

Keith

Stoitcho Goutsev (100) said:
Keith,

The type of the object that you put in the clipboard has to be
serializable. Add the [Serializable] attrinute above the class
declaration and the sample will work.


--
Stoitcho Goutsev (100) [C# MVP]

Keith said:
All,

It seems that there has been some discussion of this issue, but I can't
find any real answers. How does one copy a custom object onto the
clipboard and then retrieve it? I can do this with standard types and
collections of standard types, but if I use a custom object the
retrieved objects are always null. For example, given the following
class:

public class MyObject : Object

{

private string m_Name = "";

public MyObject(string name)

{

m_Name = name;

}

public string Name { get { return "MyObject:"+m_Name; } }

}

this code does not successfully copy/retrieve a MyObject object usign
the Clipboard:
MyObject myObj = new MyObject("Foo");

DataFormats.Format f = DataFormats.GetFormat("MyObject");

DataObject dObj = new DataObject(f.Name, myObj);

Clipboard.SetDataObject(dObj, false);

// Get it right back out

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(f.Name))

{

MyObject stuff = (MyObject)obj.GetData(f.Name, true);

if(stuff == null)

MessageBox.Show("GetData failed for " + f.Name + "!");

else

{

MessageBox.Show("MyObject on Clipboard was: " + stuff.Name,"GetData
successful!");

}

}

The GetDataPresent call is successful, but a null reference always comes
back from GetData; the autoConvert param doesn't seem to have any
affect. Do custom objects have to implement some interface or derive
from some base clsss to make this possible? Must they be cloneable?
Serializable?

Any advice or suggestions?

Keith
 

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