Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Puting a class on the clipboard

I'm having a problem reading from the clipboard. I put a class and a text version of it on the clipboard. When I read it back the text version is fine but the class is null. Any ideas?

private void SendToClipBoard()
{
CardCollection cards = new CardCollection();
foreach (ListViewItem item in listCards.SelectedItems)
{
//Add Selected Items to CardCollection
cards.Add(doc.Cards[Int32.Parse(item.SubItems[SubItemId].Text)]);
}
IDataObject iData = new DataObject();
iData.SetData(CardCollection.Format.Name, false, cards);
iData.SetData(DataFormats.Text, false, cards.ToString());
Clipboard.SetDataObject(iData, true);
}

private void ReadFromClipBoard()
{
CardCollection cards;
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
MessageBox.Show(iData.GetData(DataFormats.Text).ToString());
}
if (iData.GetDataPresent(CardCollection.Format.Name))
{
cards = (CardCollection) iData.GetData(CardCollection.Format.Name);
//Accessing the Count property throws a NullReferenceException!
if (cards.Count > 1) { doc.Cards.AddRange(cards); }
else if (cards.Count > 0) { doc.Cards.Add(cards[0]); }
}
}
[1358 byte] By [snoopy] at [2007-11-11 8:47:12]
# 1 Re: Puting a class on the clipboard
See if this helps: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms.controls/browse_thread/thread/57e50ffdd3314ccb/8c572b337db26978
Phil Weber at 2007-11-11 21:47:05 >
# 2 Re: Puting a class on the clipboard
thanks. the problem was that i forgot to make one of the classes that it used serializable.
snoopy at 2007-11-11 21:47:57 >