c# - 在 C# 中扩展窗体

标签 c# winforms

对于我的申请,我需要几种形式。这些表格都需要有一定的图片,需要有白色的底色。

不是在 VisualStudio 中创建标准表单并编辑每个单独的表单以具有这些规范,有没有一种方法可以制作我自己的每次都可以使用的表单?也许通过扩展它?

提前致谢。

最佳答案

结合 visual studio designer 小心继承。 如果您不是所有与设计器相关的属性(BrowsableAttributeDefaultValueAttribute、...)和支持成员(例如 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/

相关文章:

c# - 在运行时创建新设置并在重启后读取

c# - 空参数的 IComparable 行为

c# - NamedPipeServerStream/NamedPipeClientStream 包装器

c# - 通过将 var 更改为 IEnumerable 来获得不同的搜索结果

c# - sql server 和 c# 中的输出参数问题

c# - DataGridView 可以用来在WinForms 中完成如下类似表格的UI 吗?

c# - 使用内部应用程序的模型将公共(public)表单提交到安全的内部应用程序

c# - 如何使用 Windows 窗体创建 AutoCAD 图层?

c# - 如何以编程方式突出显示 DateTimePicker 中的特定部分?

WPF 与 Windows 窗体