Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display multiple forms when C# application starts

Tags:

c#

winforms

I have 3 forms in my C# application -- Form1, Form2 and Form3. Currently when my application starts it loads up Form1. I want all three forms to open on application startup.

I tried doing this in Program.cs:

static void Main()
{     
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    Application.Run(new Form2());
}

but Form2 only shows up after Form1 is closed.

How can I make all 3 forms show up simultaneously as soon as the application starts?

like image 875
Casper_2211 Avatar asked Nov 15 '12 20:11

Casper_2211


2 Answers

Generally, the right way to have your application do something other than the default (open a form, wait for it to close, then exit) is to make a class that inherits from ApplicationContext. Then you pass an instance of your class to the Application.Run method. When the application should close, call ExitThread() from within your class.

In this case, you can create instances of the three forms when the application loads, and register a handler for their Closed events. When each form is closed, the handler will check if there are any other forms still open, and if not will close the application.

The example on MSDN is doing two things:

  1. opening multiple forms and exiting the application when they are all closed
  2. saving the last size and position of the form when each form is closed.

A simpler example, which closes the application only after all the forms are closed:

class MyApplicationContext : ApplicationContext {
    private void onFormClosed(object sender, EventArgs e) {
        if (Application.OpenForms.Count == 0) {
            ExitThread();
        }
    }

    public MyApplicationContext() {
        //If WinForms exposed a global event that fires whenever a new Form is created,
        //we could use that event to register for the form's `FormClosed` event.
        //Without such a global event, we have to register each Form when it is created
        //This means that any forms created outside of the ApplicationContext will not prevent the 
        //application close.

        var forms = new List<Form>() {
            new Form1(),
            new Form2(),
            new Form3()
        };
        foreach (var form in forms) {
            form.FormClosed += onFormClosed;
        }

        //to show all the forms on start
        //can be included in the previous foreach
        foreach (var form in forms) {
            form.Show();
        }

        //to show only the first form on start
        //forms[0].Show();
    }
}

Then, your Program class looks like this:

static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyApplicationContext());
    }
}

The application closing logic can obviously be customized - are any forms still open, or only one of these three types, or only the first three instances (that would require holding a reference to the first three instances, possibly in a List<Form>).

Re: global event for each form creation -- this looks promising.

A similar example here.

like image 162
Zev Spitz Avatar answered Sep 19 '22 03:09

Zev Spitz


Start the other forms from the Form.Load event of Form1.

private void Form1_Load(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Show();
}
like image 22
Albin Sunnanbo Avatar answered Sep 21 '22 03:09

Albin Sunnanbo