对于我的申请,我需要几种形式。这些表格都需要有一定的图片,需要有白色的底色。
不是在 VisualStudio 中创建标准表单并编辑每个单独的表单以具有这些规范,有没有一种方法可以制作我自己的每次都可以使用的表单?也许通过扩展它?
提前致谢。
最佳答案
结合 visual studio designer 小心继承。
如果您不是所有与设计器相关的属性(BrowsableAttribute
、DefaultValueAttribute
、...)和支持成员(例如 MyProperty
)的“ friend ” >、ShouldSerializeMyProperty()
、ResetMyProperty()
),重用代码和安全时间的尝试会让人头疼。
如果可以,请使用组合而不是继承。 我在设计器中设计表单和为“内容”控件创建面板方面拥有丰富的经验。
内容控件可以继承自“usercontrol”,你也可以在visual studio designer中设计它们。
之后,您可以将任何预先设计的表单与任何预先设计的控件结合起来。
您描述的简单案例:
/// <summary>
/// Form with "skeleton" common for your application.
/// </summary>
public partial class FormWithPicture : Form {
private Control content;
/// <summary>
/// The control which should be used as "main" content of this form.
/// </summary>
public Control Content {
get { return this.content; }
set {
this.content = value;
// the "panel1" is the container of the content
this.panel1.Controls.Clear();
if (null != value) {
this.panel1.Controls.Add(value);
}
}
}
public FormWithPicture() {
InitializeComponent();
}
}
// somewhere else
new FormWithPicture {
Content = new ContentControl_A()
}.Show();
关于c# - 在 C# 中扩展窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924833/