How to get the size of an inherited form in the ba

2020-02-07 05:23发布

问题:

Suppose you have a form called FormBase and all other forms inherit from this form.
For example, I have public class Form formTest : FormBase

What I have now in the ctor of formTest:

public class Form formTest : FormBase
{
    public formTest()
    {
        InitializeComponent();
        Program.MainForm.AddToFormSizes(this, this.Size);
    }
}

This code adds the instance of formTest to a dictionary on the mainform with its size

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);
    }
}

Now, the problem is that when I do that, size will have the size of FormBase in design-time, not the size of formTest.

Is there a way in FormBase to capture the size of formTest or any other form that inherited from FormBase?

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);
}

回答1:

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()

Given the constructor of a Form derived from FormBase:

public class FormDerived : FormBase
{
     public FormDerived() => InitializeComponents();    
}

when the class is first created:

FormDerived derived = new FormDerived();
derived.Show();

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 will return the Size of the base class, not the derived class.

The FormDerived constructor is called next. At this point, all the properties defined in the InitializeComponent() method will be set.

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.

The AddToFormSizes() 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();

will cause a call to FormBase.OnHandleCreated().

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.

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);
    }
}