Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipboard Copying objects to and from

I am trying to copy an object onto the windows clipboard and off again. My code is like this:

Copy on to clipboard:

Clipboard.Clear();
DataObject newObject = new DataObject(prompts);
newObject.SetData(myString);
Clipboard.SetDataObject(newObject);

Where prompts is a List<Data.Sources.PromptResult> collection.

Copy off clipboard:

IDataObject dataObject = System.Windows.Forms.Clipboard.GetDataObject();
if (dataObject.GetDataPresent(typeof(List<Data.Sources.PromptResult>)))
{
  Type type = typeof(List<Data.Sources.PromptResult>);
  Object obj = dataObject.GetData(type);
  return (List<Data.Sources.PromptResult>)dataObject.GetData(type);
}

The GetFormats() shows the format as being in the list and the GetDataPresent(List<Data.Sources.PromptResult>) returns true but if I try to get the object out of the Clipboard class with GetData(List<Data.Sources.PromptResult>) I get a return of null.

Does anyone have any idea what might be wrong?

like image 752
James Avatar asked Jan 27 '12 11:01

James


People also ask

How do you copy an object to the clipboard?

Right-click the object to open a menu. From the menu, point to Edit, then select Copy to copy that object to the clipboard. To paste the object, right-click a blank area in the model to open a menu. From the menu, point to Edit, then select Paste.

How do you copy and paste in Portal?

In the Portal Catalog, in the context menu of the object or folder you want to copy, choose Copy . From the context menu of the destination folder, choose Paste . The Paste wizard appears.

How do you copy an object in Vectorworks?

Copying Objects The original object remains on the drawing. To copy an object: Select the object to copy. Select Edit > Copy.


2 Answers

OK I tried to add list of my user type to clipboard and get it back... Here is what I tried:

My User Class:

public class User
{
   public int Age { get; set; }
   public string Name { get; set; }
}

Rest of Code:

// Create User list and add some users
List<User> users = new List<User>();
users.Add(new User { age = 15, name = "Peter" });
users.Add(new User { age = 14, name = "John" });

// Lets say its my data format
string format = "MyUserList";
Clipboard.Clear();

// Set data to clipboard
Clipboard.SetData(format, users);

// Get data from clipboard
List<User> result = null;
if (Clipboard.ContainsData(format))
    result = (List<User>)Clipboard.GetData(format);

...and result was null :) ...until I marked User class as Serializable

[Serializable]
public class User
{ 
    //...   
}

After that my code worked. Ok its not the answer but maybe it helps you some how.

like image 79
Renatas M. Avatar answered Oct 13 '22 06:10

Renatas M.


@Reniuz thanks for your help it has helped me to work out the answer.

In order to get the text and custom object data out of the Clipboard with multiple formats I have implemented the IDataObject interface in my own class. The code to set the data object must have the copy flag set like this:

Clipboard.Clear();
Clipboard.SetDataObject(myClassThatImplementsIDataObject, true);

To get the data out again the standard text can be retrieved using:

Clipboard.GetText();

The data can be retrieved using the data method:

Clipboard.GetData("name of my class");

The other point that was helpful was to test that the object I was putting into the clipboard could be serialized by using the BinaryFormatter class to perform this test... If an exception is thrown that copying to the clipboard would also fail, but silently.

So my class has:

[Serializable]
public class ClipboardPromptsHolder : IDataObject
{
    ...
}
like image 44
James Avatar answered Oct 13 '22 06:10

James