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?
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.
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.
Copying Objects The original object remains on the drawing. To copy an object: Select the object to copy. Select Edit > Copy.
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.
@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
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With