473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Cut/Copy/Paste in C#

Hi,

I wanted to implement cut/copy/paste operation in C# using ClipBoard class.
Eg: Wanted to store the Employee object during cut/copy and perform paste to show in UI.
anybody throw lights on how to store the customized class object using ClipBoard class.

Thanks,
May 6 '08 #1
7 11915
Plater
7,872 Expert 4TB
You should look at the msdn doc's on it:

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. Clipboard.SetData(DataFormats.Serializable,myClassInstance)
  2.  
May 6 '08 #2
Thanks for the info.

I have tried using Clipboard.SetData using DataFormats.Serializable and added my own class(X) object. Ensured that my class X having all the members as Serializable attribute defined.
But the problem is when i use IDataObject GetData for DataFormats.Serializable returns null. IDataObject GetDataPresent() returns true indicating that info available.

Also I tried otherway:
Clipboard.SetDataObject(myDataObject) with my own DataFormats.
IDataObject GetData() returns null for my own Dataformats. however Clipboard.ContainsData(MydataFormat.Name) returns ture indicating the data present.

Any hint abt what is going wrong here
May 8 '08 #3
Plater
7,872 Expert 4TB
Shouldn't you be using Clipboard.GetData() to get your data back?
May 8 '08 #4
Clipboard.GetData() also returns NULL ( i have done casting for GetaData to my own object)
May 9 '08 #5
Plater
7,872 Expert 4TB
Ok, I managed to find an example and piece it together.

Expand|Select|Wrap|Line Numbers
  1. public static class Copying
  2. {
  3.     public static bool IsSerializable(object obj)
  4.     {
  5.       MemoryStream mem = new MemoryStream();
  6.       BinaryFormatter bin = new BinaryFormatter();
  7.       try
  8.       {
  9.       bin.Serialize(mem, obj);
  10.       return true;
  11.       }
  12.       catch (Exception ex)
  13.       {
  14.       MessageBox.Show("Your object cannot be serialized." +" The reason is: " + ex.ToString());
  15.       return false;
  16.       }
  17.     }
  18.     public static void CopyToClipboard(object obj)
  19.     {
  20.       //register my custom data format with Windows
  21.       //or get it if it's already registered
  22.       DataFormats.Format format = DataFormats.GetFormat(obj.GetType().FullName);
  23.  
  24.       //now copy to clipboard
  25.       IDataObject dataObj = new DataObject();
  26.       dataObj.SetData(format.Name, false, obj);
  27.       Clipboard.SetDataObject(dataObj, true);
  28.  
  29.       //that's it
  30.     }
  31.     public static object GetFromClipboard(Type T)
  32.     {
  33.       object doc = null;
  34.       IDataObject dataObj = Clipboard.GetDataObject();
  35.       string format = T.FullName;
  36.  
  37.       if (dataObj.GetDataPresent(format))
  38.       {
  39.       doc = dataObj.GetData(format);
  40.  
  41.       }
  42.       return doc;
  43.     }
  44. }
  45.  
I tested it with this object
Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2. public class Document
  3. {
  4.    public int documentID = 0;
  5.    public string documentDescription = "";
  6. }
  7.  
Then used this:
Expand|Select|Wrap|Line Numbers
  1. Document myobj = new Document();
  2. myobj.documentDescription= "Fred";
  3. myobj.documentID = 4;
  4. bool canSet=Copying.IsSerializable(myobj);
  5. if(canSet)
  6. {
  7.    Copying.CopyToClipboard(myobj);
  8.    object b = Copying.GetFromClipboard(typeof(Document)); 
  9. }
  10.  
"b" was now the instance of the Document object with "fred" and 4 as it's memeber values.
May 9 '08 #6
Thank you very much for sample.
Object i'm setting to Clipboard is complex one.

It is throwing exception of kind "You must implement a default accessor on NameSpace.DataCollection because it inherits from ICollection."

This exception thrown when i was trying to cross check serialization works or not using

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(Myobj.GetTy pe());

This throwed exception as above. So now i feel MyObj (internally having member to DataCollection class) having certain issues.
May 9 '08 #7
Plater
7,872 Expert 4TB
Here's the link to the example I used:
http://www.codeproject.com/KB/cs/cop...toclipbrd.aspx

Maybe you can pull more out of it.
May 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: The Flyer | last post by:
Hi Sorry for posting this message to so many groups, but I was not sure which group the windows keyboard experts would be reading, so I ventured to post it to all of them. I want to map or...
3
by: Faith | last post by:
Hello. I need to take a column from Excel(unknown amount of rows) that will be selected by the user and copy those cells. Then I will need to paste those cells into the first column in a Data...
4
by: Legendary Pansy | last post by:
I was checking out the 101 C# Samples, specifically Windows Forms - Use the Clipboard. I took a look at the code for a while, and I understand what the program is doing with the cut, copy, pasting...
3
by: Rachel Suddeth | last post by:
This may not be the right forum, but it's a problem I chiefly come across when trying to post here. When I do a copy/paste from VS, the text always looks really weird (and even if I'm in an...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: NetronProject | last post by:
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types. All entries in the CollectionBase...
0
by: squirrelonfire | last post by:
Hi I run postgreSQL from a remote server like this SSH -X cnguyen_db@okaram.spsu.edu ....enter the password Then I run bluefish& and psql The problem I'm having is that I can't copy and paste...
2
by: al_johnson222 | last post by:
>From any page, I want to be able to call a JS function that will do the equivelant of select all, and copy. This data will then be posted to a page that will log it. This would be easy using...
8
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
3
by: questionit | last post by:
Some websites disable the Windows copy and paste feature to use on thier website. A text can not be copied and pasted either from using shortcuts : CTRL+C, CTRL+V or from using copy, paste option...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.