Activate Main Form

  • Thread starter Thread starter Bill Byrnes
  • Start date Start date
B

Bill Byrnes

I have a small windows form application. I have a main form and a second
form that I open from the main form menu.

The second form opens in a fully maximized windows. I cannot figure out how
to activate or show the main form from the second forms menu (ie, what code
to put in the menu event). I can actiate the second form easily becasue
there is a variable used to create it:

SecondForm secondForm=new SecondForm();
secondForm.Show();

But the way the main (startup) form is initialized, there is no variable.

Any help is appreciated.

Bill Byrnes
 
One way to do this is to create a constructor overload in SecondForm that
accepts a Form...

public class SecondForm : Form
{
private Form mainForm = null;

public SecondForm (Form mainForm)
{
this.mainForm = mainForm;
}

// And then in the event handler for the menu item...
// if (this.mainForm != null)
// { this.mainForm.BringToFront(); }
}

In the main Form when creating the SecondForm instance use code like this...

SecondForm secondForm = new SecondForm(this);
secondForm.Show();
 
Back
Top