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

Re: Putting custom objects on the clipboard by Stoitcho

Stoitcho
Fri Apr 29 19:49:52 CDT 2005

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" <keith@alh.com> wrote in message
news:OrbcLObSFHA.1972@TK2MSFTNGP10.phx.gbl...
> 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
>



Re: Putting custom objects on the clipboard by Keith

Keith
Tue May 03 10:36:50 CDT 2005

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) [C# MVP]" <100@100.com> wrote in message
news:e8GYE6RTFHA.2996@TK2MSFTNGP15.phx.gbl...
> 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" <keith@alh.com> wrote in message
> news:OrbcLObSFHA.1972@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>
>



Re: Putting custom objects on the clipboard by Stoitcho

Stoitcho
Fri May 06 17:12:28 CDT 2005

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" <keith@alh.com> wrote in message
news:uJi0IZ$TFHA.3636@TK2MSFTNGP14.phx.gbl...
> 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) [C# MVP]" <100@100.com> wrote in message
> news:e8GYE6RTFHA.2996@TK2MSFTNGP15.phx.gbl...
>> 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" <keith@alh.com> wrote in message
>> news:OrbcLObSFHA.1972@TK2MSFTNGP10.phx.gbl...
>>> 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
>>>
>>
>>
>
>



Re: Putting custom objects on the clipboard by Stoitcho

Stoitcho
Fri May 06 17:14:46 CDT 2005

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" <keith@alh.com> wrote in message
news:uJi0IZ$TFHA.3636@TK2MSFTNGP14.phx.gbl...
> 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) [C# MVP]" <100@100.com> wrote in message
> news:e8GYE6RTFHA.2996@TK2MSFTNGP15.phx.gbl...
>> 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" <keith@alh.com> wrote in message
>> news:OrbcLObSFHA.1972@TK2MSFTNGP10.phx.gbl...
>>> 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
>>>
>>
>>
>
>