Share via


How do I open multiple windows/forms in C#?

Question

Saturday, October 24, 2009 7:02 AM

Hello,

Here's the thing:
I've got a Program.cs, which contains Main()
Main() contains "Application.Run(new Form1());"
Form1.cs contains a button.
If that button is to be pressed, Form1 should shutdown, and Form2 should be run.

How do I do so?

Best Regards,
Etienne Bruines

All replies (6)

Saturday, October 24, 2009 1:01 PM ✅Answered

Project + Add Reference, select Microsoft.VisualBasic.  Make your Program.cs source code file look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WindowsFormsApplication1 {
  class Program : WindowsFormsApplicationBase {
    [STAThread]
    static void Main(string[] args) {
      new Program().Run(args);
    }
    public Program() {
      this.EnableVisualStyles = true;
      this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;
      this.MainForm = new Form1();
    }
  }
}

Now you can close your main form, your program won't terminate until the last form closes.Hans Passant.


Saturday, October 24, 2009 1:23 PM ✅Answered

        private

 void

 btnOpenForm2_Click(object

 sender, EventArgs e)
        {
            Form2 frm2 = new

 Form2();
            frm2.Show();
            frm2.FormClosed += new

 FormClosedEventHandler(frm2_FormClosed);
            this

.Hide();
        }

        void

 frm2_FormClosed(object

 sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

if you want you applicaiton to show Form1 rather then closing application then use

this.Show();

instead of Application.Exit() in frm2_FormClosed event

Is it also possible to close previous forms?
Because, my feeling tells me, that when I return to Form1, and then go to Form2 again, I define "frm2" twice, and will result in an error?
Plus, it's better for your memory, if not all forms are loaded together, right?

in this case , i am hiding Form1 because Form1 is specified in Application.Run(new Form1()); statement in Program.cs file.
if i close Form1 rather then hiding, then Application will exit.

in current example , if you specify this.Show() in frm2_FormClosed event to move back to Form1 like

        void
 frm2_FormClosed(object
 sender, FormClosedEventArgs e)

        {

            this.Show();

        }

then Form2 will be Disposed and Form1 (which was in memory) is shown.

However if you want only one instance of any window in memory (either Form1 or Form2 in memory at a time) , then
in Program.cs file you have to open Form1 as

new Form1().Show();
Application.Run();

then to open Form2 use , in this case Form2 will open and Form1 will be Closed.

        private void btnOpenForm2_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
            this.Close();
        }



        void frm2_FormClosed(object sender, FormClosedEventArgs e)

        {

            Form1 frm1 = new Form1();

            frm1.Show();

        }

in this case only Form will be in memory at a time, however even you close both Forms your applicaion process is still in memory , you have to handle this, either providing "close" button and calling Application.Exit();


Saturday, October 24, 2009 8:10 AM

You can open as many forms on the button click event as you want.  I'm assuming that "a" is your current form.

FormB b = new FormB();
BormC c = new FormC();
FormD d = new FormD();

b.Show();
c.Show();
d.Show();
a.Close();


Saturday, October 24, 2009 10:50 AM

        private void btnOpenForm2_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
            this.Hide();
        }

        void frm2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

if you want you applicaiton to show Form1 rather then closing application then use

this.Show();

instead of Application.Exit() in frm2_FormClosed event


Saturday, October 24, 2009 12:01 PM

You can open as many forms on the button click event as you want.  I'm assuming that "a" is your current form.

FormB b = new FormB();
BormC c = new FormC();
FormD d = new FormD();

b.Show();
c.Show();
d.Show();
a.Close();

b c and d become part of a.
b will be:   a.b
c will be:   a.c
So, when closing a:  b c and d will close as well...
Any other options?


Saturday, October 24, 2009 12:05 PM

        private
 void
 btnOpenForm2_Click(object
 sender, EventArgs e)
        {
            Form2 frm2 = new
 Form2();
            frm2.Show();
            frm2.FormClosed += new
 FormClosedEventHandler(frm2_FormClosed);
            this
.Hide();
        }

        void
 frm2_FormClosed(object
 sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

if you want you applicaiton to show Form1 rather then closing application then use

this.Show();

instead of Application.Exit() in frm2_FormClosed event

Is it also possible to close previous forms?
Because, my feeling tells me, that when I return to Form1, and then go to Form2 again, I define "frm2" twice, and will result in an error?
Plus, it's better for your memory, if not all forms are loaded together, right?