-3

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;
        }
Tom1
  • 77
  • 1
  • 6

1 Answers1

0

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
Ian H.
  • 3,840
  • 4
  • 30
  • 60
Paweł Swajdo
  • 391
  • 1
  • 13
  • Please at least format your code properly – Camilo Terevinto Jan 31 '17 at 14:05
  • yes I understand how to make it load for each form but how would I make this only work if a previous condition is true – Tom1 Jan 31 '17 at 14:06
  • Oh I see now. But here in this method you can use if(WindowState == FormWindowState.Maximized) – Paweł Swajdo Jan 31 '17 at 14:10
  • and if you really need to know if the button was pressed (windowState is not important, only the fact of clicking button) then I think you need to set some kind of global variable that informs you button was clicked. But this is rather strange solution. – Paweł Swajdo Jan 31 '17 at 14:15
  • yeah thats what i want to do but i also want to avoid global variables. I think the reason people didnt understand me was because i barely understood my own problem :/ – Tom1 Jan 31 '17 at 14:18