Cut and paste BOs?

Cut and paste BOs?

Old forum URL: forums.lhotka.net/forums/t/2648.aspx


ajj3085 posted on Monday, April 02, 2007

Hi all,

I have a requirement where users would like to be able to cut and paste some elements of a child collection from one BO to another (i.e., cut & paste line items from one quote to another).

It seems to me this would be a case of copying the relevent BOs to the clipboard then removing them from the source document.  Pasting would likewise remove the BOs from the clipboard and add them to the target document.

Has anyone used the clipboard for this before?  I don't have any experience with it myself, and its not clear if using it in this way would cause any problems with Csla.

Thanks
Andy

SonOfPirate replied on Monday, April 02, 2007

Andy,

I did it a while ago with version 1.x but haven't tried it with 2.x (yet).  When I did it, I was surprised how easy it was to accomplish.  You should be able to simply add the BO to the clipboard (Clipboard.SetObjectData(...))and retrieve it (Clipboard.GetObjectData()) intact.  You'll have to add your own interface to manage this, of course, to verify that the object is of the right type, etc. before pasting and so on.  But, I think you'll find it pretty straight-forward to implement the cut & paste part - it's the rules governing how it works that could get complicated.

Keep in mind that the object in the clipboard and all instances retrieved will be copies of the original.  This means that you could keep pasting the same line item multiple times - and in multiple applications.  Not much you can do to prevent the latter but if you want to be able to paste it into Excel, for instance, you may have to change how you put the object into the clipboard to make it compatible (serialize as XML, perhaps).  If the former is a concern, you'll want to put logic in to remove the item from the clipboard when pasted so it can only be done once.

HTH

 

ajj3085 replied on Monday, April 02, 2007

Hey Pirate,

Thanks for the feedback, very useful.  Some more questions though..

First, how does the Clipboard know how to clone the object?  Does this process trigger any Csla events?  Second, I'm only interested in copying and pasting between a single instance of my own application.  Since my application prevents multiple instances, it seems that I don't need to do anything keep other applications from being able to copy / paste line items into or out of my application.  Is that correct?

Thanks
Andy

jeff replied on Monday, April 02, 2007

If you only want your copied objects available to your own application then bypass the windows clipboard altogether and just create some static field somewhere for your copy/paste functionality.

SonOfPirate replied on Monday, April 02, 2007

I would agree with Jeff.  The only reason I can see making use of the Windows Clipboard is if you want to be able to cut & paste between applications.  Sure it makes doing the same thing easier since it already exists, etc. but you would get greater control with a custom version and not have to deal with someone placing your LineItem object in the clipboard then clicking paste in Word or something.

As for your questions, I actually don't know how the Clipboard works.  I assume it is just a straight binary copy handled by the OS.  As such, I don't believe there are any events or callbacks that would notify your object that it has been copied to or pasted from the clipboard.  It's all up to you to implement that in your application.  Kind of like the difference between a copy and cut operation is that you remove the object (in your application) after placing it in the clipboard for the latter.

HTH

 

ajj3085 replied on Tuesday, April 03, 2007

Jeff and Pirate,

Thanks both for your feedback.  I guess I'll go the manual route, since I'd have to solve problems presented by the clipboard anyway.

My idea is now to use a static variable in the form to track the item to be copied / cut.

Andy

Copyright (c) Marimer LLC