How can I copy data of a usercontrol when usercontrol itself is copied RRS feed

  • Question

  • I am currently writing a VSTO Excel "Application-Level" Add-In using C#.

    I have written a quite complex UserControl, that has several properties, which can be changed at runtime.

    Therefore I defined a UserControlData class which is private to my UserControl.

    I managed to store all this data in the CustomXmlParts section of the workbook.

    However I can not find out how to pass this information to the duplicated control when using ctrl-c/x ctrl-v.

    Is there a concept for this task?

    Regards, Helmut

    Tuesday, January 13, 2015 1:38 PM

All replies

  • I'm afraid you can't do that with the build-in methods. maybe it's possible by monitoring the key press and the clipboard, but that's not related to office development, it's all about windows or .net development.
    Wednesday, January 14, 2015 8:38 AM
  • I found out, that it is even worse:

    Whenever a control is duplicated by entering the Excel design-mode and copy/pasting it, the control itself is not duplicated.
    Instead only an image of the control is pasted. I read, that an ActiveX-Host is defined, to host user controls. So only this host is duplicated, but not the control itself.
    It would have been better to forbid coying usercontrols in general, because this result was never intended!
    But since this can be done, I want my usercontrol to behave as expected.

    So does anybody know a technique to construct the usercontrol itself, whenever a copy/paste operation is performed?
    I am able to hook into the keyboard to trap a ctrl-c/ctrl-v, but this does not help much, since the user can use the regular copy paste command from the menu.
    Furthermore it is not so easy to find out, that a usercontrol is selected in design mode.

    Thursday, January 15, 2015 5:12 PM
  • Hi Caer_Dalben,

    If you mean you want to copy the UserControl at runtime, my suggestion is that when your add-in detect the Copy action(ctrl-c) action, save the current UserControl's properties in some temporary place(some temp file in system's temp folder). When detect the Paste(ctrl-v) action,  create a new UserControl and restore the data. 

    If you mean you want to do this at development time in Visual Studio, I think it's hard. I'm not aware of any way to do this. You can initialize a new UserControl instance and the properties in code, maybe this is the best way. 

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 3:24 PM
  • Hi Caillen,

    Whenever the user enters the Excel-designmode and copies a user-control, the result is just a bitmap and not a duplicated instance of the control.

    Even if the control is not much more than a stupid button, its functionality is lost afterwards. Forthermore after saving and reopening of the workbook, the copied instances have vanished.

    I can not imagine a scenario where this is what the user expects to do. If this is all what happens, it would have been better to forbid copying of usercontrols in general.

    My problem is, that I did not find a way to recognise that a copy / paste action has been performed.

    If there is a way, it would be easy to write/read from the clipboard on my own. I then would have the posibility to duplicate the control in code behind.

    But without any triggers, I have no chance to implement a reliable behavior here.

    All this is very enoying, since it is not possible to extend Excel with self written usercontrols, that behave as expected. Thus the whole user-control concept of the VSTO is useless in my eyes. And I can not believe that this is all Microsoft comes up with.

    Regards, Helmut

    Monday, February 2, 2015 3:52 PM