I need to implement Cut/Copy/Paste functionality within an application, but not for use with Excel - it is in the application only.
I'm handling the BeforePerformAction event and handling the cut/copy/paste actions, and serializing my custom type to/from the clipboard manually. The event is firing as expected, and the clipboard operations work correctly (outside of the grid). However, when I put the two together, it doesn't seem to be working quite right.
I first tried cancelling the action after copying to the clipboard, but if I do that, then the paste action never fires. If I don't cancel the action after copying, then it looks like the grid is putting it's own data into the clipboard (haven't had a chance to actually look at the data here yet though). Is there some better way to implement this? Should I just handle the keypresses directly? -- though if I do this, I may have problems later with toolbar menu items.
Hi Vineas,
If you cancel the BeforePerformAction, then the grid will not post anything to the clipboard. You can, of course, post your own data to the clipboard, but you obviously can't expect the grid to recognize your custom data format. If you paste into the grid at this point, I would think that the BeforePerformAction would still fire, but it sounds like you are saying it does not. I guess the grid must be examining the contents of the clipboardbefore firing the BeforePerformAction and determining that there is nothing on the clipboard that the grid can recognize. This seems a little odd. One might argue that the grid should fire the event first before checking the clipboard contents. But then the event would fire when no action is really being performed, so it's a little odd either way. Changing the firing of the event now would break existing applications, so it's not something that can be changed at this point.
On the other hand, if you don't cancel the BeforePerformAction, then the write some data to the clipboard, the grid will then go ahead and write it's data to the clipboard after you are done. The Clipboard object can store data in multiple formats, though. So you might be able to specify a format for you data that the grid does not use. I'm not sure this will work. The grid might be clearing the contents of the clipboard before it starts writing, but if that's the case, I think that would be a bug.
Another potential solution would be to use a different event. Perhaps you can use BeforeMultiCellOperation, instead. This event might have the same issues, though, I'm not sure.
You definitely described the behavior I'm seeing, and it definitely looks like the grid is checking out the clipboard's contents before a paste - using reflector, before an action is performed, there is a check to see if the action is allowed. This is where the paste is being killed. Unfortunately, this is the spot that is stopping my code from working. I'm going to start looking into a way to override this behavior - which looks like it may be possible since the KeyActionMappings property is public (any pointers in this area would be appreciated).
After finding and using a clipboard viewer program, I don't think it's the case that the grid is clearing the clipboard before writing anything - it's just that the grid writes in so many formats that it may as well do just that. As far as I can tell, the grid writes in any format that the custom type could be written in. As far as the BeforeMultiCellOperation event is concerned - it is raised after the BeforePerformAction, so ends up having the same problems - probably as a simple side-effect of when it is raised.