Copying control components

邮差的信 提交于 2019-12-25 00:46:54

问题


I have a C++/CLI project which has a child form with some control components. I would like to copy all the control components with their codes on a new Tab control on the main form. How can I do that?


回答1:


You can select all of the controls in the designer (hold down the Shift key to make multiple selections), and then cut and paste them to their new location: Ctrl+X, Ctrl+V

Alternatively, you can open up the *.Designer file that is generated automatically by the Windows Forms designer and copy the relevant lines of code out of that file into your other code file. Look for the code that creates and sets properties on the controls you're interested in. For every property of the control that you have set to its non-default value in the designer, there will be a line for it in this file.

If, instead of moving (cutting) the controls from one place to another, you want to copy the controls so that they appear in multiple places, I strongly suggest creating a UserControl that contains all of the child controls and encapsulates the relevant code. Then, you can just drop an instance of this custom UserControl out of your toolbox into whatever form you want, or even dynamically create instances at runtime. This is consistent with the general programming/design principle that you should seek to keep code duplication to a minimum while encapsulating as much as possible.



来源:https://stackoverflow.com/questions/9317069/copying-control-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!