复制控制组件 [英] Copying control components

查看:156
本文介绍了复制控制组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ / CLI项目,它有一个子窗体和一些控件组件。我想在主窗体上的一个新的Tab控件上复制所有控件组件及其代码。

解决方案

您可以选择设计器中的所有控件(按住 Shift 键进行多项选择),然后将其剪切并粘贴到新位置: Ctrl + X Ctrl + < > V



或者,您可以打开自动生成的 *。Designer Windows窗体设计器并将该文件中的相关代码行复制到其他代码文件中。查找在感兴趣的控件上创建和设置属性的代码。对于在设计器中设置为其非默认值的控件的每个属性,在此文件中将有一行。 / p>

如果不是将控件从一个地方移动到另一个地方,而是要复制所以它们出现在多个地方,我强烈建议创建一个包含所有子控件并封装相关代码的 UserControl 。然后,您可以将该自定义 UserControl 的实例从您的工具箱中删除为任何形式,或者甚至在运行时动态创建实例。这与一般的编程/设计原则是一致的,你应该尽可能地将代码复制减少到最小。


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?

解决方案

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.

这篇关于复制控制组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆