1

So I'm trying to copy and paste an object and having trouble getting it right. I've searched through the topics but I still can't seem to get it to work. Here is the code:

In one solution in Visual studio I have the the class:

namespace test4
{
    [Serializable]
    public class copypaste
    {
        public string test = "a";
    }
}

and the copy part of the code:

private void btn1_Click(object sender, EventArgs e)
{
        var copy_obj = new copypaste();
        DataObject d = new DataObject(copy_obj);
        Clipboard.SetDataObject(d);
}

And in another solution I have:

namespace test4
{
    [Serializable]
    public class copypaste
    {
        public string test = "a";
    }
}

and the paste part of the code:

private void btnTest_Click(object sender, EventArgs e)
{
    var d = Clipboard.GetDataObject();
    if (d.GetDataPresent("test4.copypaste"))
    {
        var o = d.GetData("test4.copypaste");
        Debug.WriteLine( ( (copypaste)o ).test );
    }
}

However, I end up with the following error on the final line: 'System.InvalidCastException: 'Unable to cast object of type 'System.IO.MemoryStream' to type 'test4.copypaste'.'

I have gone through other questions which suggest this way of copy/pasting code but none seem to return memory stream when they call the GetData method. I am unsure how to extract the object from the memory stream.

Thanks

  • you haven't streamed the object to a MemoryStream. Your code is not serializing anything.. you are simply putting an object in the clipboard. – T McKeown Mar 13 '19 at 15:58
  • Afair you have to use [SetData](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setdata) if you use `GetData` with custom format – Sinatr Mar 13 '19 at 16:00
  • You need to define the custom dataFormat when saving the data to the clipboard. Check the example here: https://docs.microsoft.com/it-it/dotnet/api/system.windows.forms.dataformats?view=netframework-4.7.2 In your case it would be something like: DataFormats.Format myFormat = DataFormats.GetFormat("test4.copypaste"); var copy_obj = new copypaste(); DataObject myDataObject = new DataObject(myFormat.Name, copy_obj); Clipboard.SetDataObject(myDataObject); – Fabio M. Mar 13 '19 at 16:00

1 Answers1

2

With this reference in mind and with your serializable class, this works as expected:

private void copyButton_Click(object sender, EventArgs e)
    {
        DataFormats.Format myFormat = DataFormats.GetFormat("test4.copypaste");
        var copy_obj = new copypaste();

        DataObject myDataObject = new DataObject(myFormat.Name, copy_obj);
        Clipboard.SetDataObject(myDataObject);
    }

    private void pasteButton_Click(object sender, EventArgs e)
    {
        var d = Clipboard.GetDataObject();
        if (d.GetDataPresent("test4.copypaste"))
        {
            var o = d.GetData("test4.copypaste");
            Debug.WriteLine(((copypaste)o).test);
        }
    }
Fabio M.
  • 184
  • 1
  • 8
  • To me it works like a charm. Check this out: [link](https://imgur.com/5MVZR6s). In this new example I just added a constructor to your serializable class: public copypaste(string t) { test = t; } and then, in copyButton_Click I changed the copy_obj instantiation with this: var copy_obj = new copypaste(copyTextBox.Text); In pasteButton_Click instead changed the Debug.WriteLine with: pasteLabel.Text = ((copypaste)o).test; Full code here [link](https://imgur.com/dTGSAjH) – Fabio M. Mar 14 '19 at 08:07
  • Thanks for getting back. Yes this works for me too if i I use the copy and paste functions in the same solution, my issue when i put the copy function in one solution and the paste function in another.... – user3880632 Mar 14 '19 at 08:44
  • I tried this approach in the same solution: - created a library project with the class copypaste in it - created a WinForms application project referencing the library project and put in the "copyButton" - created a WinForms application project referencing the library project and put the "pasteButton" And it works properly! Re-reading through your opening post, I think the problem is that you copy-pasted the class. For .NET they are two different classes, no matter what is their signature and/or namespace. If you need to use two solutions, you have to share a dll with the class. – Fabio M. Mar 14 '19 at 09:08
  • Ah ok, that answers that then. Thanks! – user3880632 Mar 14 '19 at 11:37