C# how do I make a procedure that is run every time a form is loaded?

104 Views Asked by At

In my c# forms project I want this method to run every time I load any of my forms.

        foreach (Form frm in Application.OpenForms)
        {
            frm.WindowState = FormWindowState.Normal;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Bounds = Screen.PrimaryScreen.Bounds;
        }
1

There are 1 best solutions below

5
On BEST ANSWER

My proposition: Create a BaseClass

public class BaseClass: Form

... and add method to it:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    foreach (Form frm in Application.OpenForms)
    {
        frm.WindowState = FormWindowState.Normal;
        frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        frm.Bounds = Screen.PrimaryScreen.Bounds;
    }
}

Add this exact base class to each of your forms, like such:

public partial class Form1 : BaseClass