问题描述
假设您有一个名为 FormBase
的表单,所有其他表单都从该表单继承。
例如,我有公共类Form formTest:FormBase
Suppose you have a form called FormBase
and all other forms inherit from this form.
For example, I have public class Form formTest : FormBase
我现在在formTest的目录中拥有的内容:
What I have now in the ctor of formTest:
public class Form formTest : FormBase
{
public formTest()
{
InitializeComponent();
Program.MainForm.AddToFormSizes(this, this.Size);
}
}
此代码添加 formTest 到主表单上具有 size
This code adds the instance of formTest
to a dictionary on the mainform with its size
的词典我想将这段代码移到 FormBase
,这样我就不必在每一个继承的表单中都放这行代码。
This works, but I would like to move this code to FormBase
so I don't have to put this line of code in every inherited form.
public class Form FormBase : Form
{
public FormBase()
{
InitializeComponent();
Program.MainForm.AddToFormSizes(this, this.Size);
}
}
现在,问题是当我这样做时, size
在设计时的大小为 FormBase
,而不是 formTest
。
Now, the problem is that when I do that, size
will have the size of FormBase
in design-time, not the size of formTest
.
FormBase
中是否可以捕获 formTest 或从
FormBase
继承的任何其他形式?
Is there a way in
FormBase
to capture the size of formTest
or any other form that inherited from FormBase
?
供参考,这是
AddToFormSizes
在MainForm中
for reference, this is the code of
AddToFormSizes
in the MainForm
private Dictionary<Form, Size> _formSizes = new Dictionary<Form, Size>();
public void AddToFormSizes(Form form, Size size)
{
_formSizes.Add(form, form.Size);
}
推荐答案
问题:
使用 Form
作为其他Form的基础,在基类构造函数中, this
引用返回基类的 Size
而不是 Size
的派生类。
Problem:
Using a Form
as base for other Forms, in the base class constructor, the this
reference returns the Size
of the base class instead of the Size
of the derived class.
public class FormBase : Form
{
public FormBase()
{
InitializeComponent();
Program.MainForm.AddToFormSizes(this, this.Size);
}
}
只需遵循以下事件顺序即可:
It's just a matter of following the sequence of events:
FormDerived derived = new FormDerived()
=> FormBase.InitializeComponent()
=> FormDerived.InitializeComponent()
derived.Show()
=> FormBase.OnHandleCreated()
给出a的构造函数从 FormBase
派生的表单:
Given the constructor of a Form derived from FormBase
:
public class FormDerived : FormBase
{
public FormDerived() => InitializeComponents();
}
首次创建该类时:
FormDerived derived = new FormDerived();
derived.Show();
基类的构造函数( FormBase
)首先被称为。
此时, 此
引用被设置为 FormDerived
,但所有属性,包括 Name
和表单的标题( Text
),设置为基类的值。
因此,调用使用 this
的方法>在基类构造函数中:
the constructor of the base class (FormBase
) is called first.
At this point, the this
reference is set to FormDerived
, but all the properties, including the Name
and the Form's Caption (Text
), are set to the values of the base class.
Hence, calling a method that uses this
in the base class constructor:
Program.MainForm.AddToFormSizes(this, this.Size);
this.Size
将返回基类,而不是派生类。
this.Size
will return the Size of the base class, not the derived class.
接下来调用 FormDerived
构造函数。此时,将设置 InitializeComponent()
方法中定义的所有属性。
The FormDerived
constructor is called next. At this point, all the properties defined in the InitializeComponent()
method will be set.
如果将 AddToFormSizes()
方法移动到派生类构造函数,则 this
将参考期望值。但是,必须在 FormBase
的每个派生类中插入静态方法调用。
If the AddToFormSizes()
method is moved to the derived class constructor, this
will reference the expected values. But, the static method call must be inserted in each derived class of FormBase
.
方法,该方法将在首次显示派生的Form时调用: AddToFormSizes()
方法可以移至覆盖的 <$ c $基类的c> OnHandleCreated()
The AddToFormSizes()
method can be instead moved to and called from the overridden OnHandleCreated()
method of the base class, which will be called when the derived Form is first shown:
derived.Show();
将导致调用 FormBase.OnHandleCreated()
。
will cause a call to FormBase.OnHandleCreated()
.
此时, this
引用已设置为派生类,并且在 InitializeComponent()
中定义的所有属性都已经设置为派生类的值。
名称,
文本
和大小
。
At this point, the
this
reference is already set to the derived class and all the properties defined in InitializeComponent()
will be already set to the values of the derived class.Name
, Text
and Size
included, of course.
此处,
此
是 FormDerived
,并在其构造函数中设置了所有属性:
Here,
this
is FormDerived
with all the properties set in its constructor:
public class FormBase : Form
{
public FormBase() => InitializeComponent();
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Program.MainForm.AddToFormSizes(this, this.Size);
}
}
这篇关于如何在基本表单中获取继承表单的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!