阻止其他winforms覆盖父winform属性

时间:2013-07-03 04:05:30

标签: c# winforms inheritance

我有一个winforms列表

mainWinform
winform1
winform2
winform3 ...

winform1winform2winform3继承mainWinform设计

我设置了一些winform,如:

winform1

maximizeBox = false
FormBorderStyle to Sizable 

winform2

maximizeBox = true
FormBorderStyle to fixed

由于我继承自mainWinform,我可以设置每个winform 设置maximizeBox = true甚至FormBorderStyle to Sizable的{​​{1}},mainWinform已将其设为winform1 maximizeBox = false

2 个答案:

答案 0 :(得分:1)

要阻止继承的表单覆盖父级设置,您可以尝试覆盖父表单类中的OnStyleChanged

protected override void OnStyleChanged(EventArgs e)
{
    if (this.MaximizeBox!=true) this.MaximizeBox = true;
    base.OnStyleChanged(e);
}

答案 1 :(得分:0)

您必须在继承的form的构造函数中设置这些属性。例如:

public winform
{
    //in this way you can inherit parent form properties and set them to child form
    this.MaximizeBox = base.MaximizeBox;
    this.FormBorderStyle = base.FormBorderStyle;
    //or if you want to set it something else, then do like this
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}

您可以根据需要更改值。但是你必须将代码放在构造函数中。