Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c# - How to get the size of an inherited form in the base form?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...