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
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