Copying control components

2019-08-29 09:51发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-08-29 10:18

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.

查看更多
登录 后发表回答