Display multiple forms when running c # application

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

I tried to do this in Program.cs :

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

but Form2 appears only after closing Form1.

How can I create all 3 forms at once as soon as the application starts?

+6
source share
4 answers

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

 private void Form1_Load(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } 
+10
source

Typically, the right way to get your application to do something different from the standard (open a form, wait for it to close, and 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 your class.

In this case, you can instantiate the three forms when the application loads and register a handler for your Closed events. When each form is closed, the handler checks for any other forms and, if it does not close the application.

The example in MSDN does two things:

  • opening several forms and exiting the application when they are all closed.
  • saving the last size and position of the form when closing each form.

The simplest example that closes the application only after closing all forms:

 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 `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()); } } 

Obviously, the logic for closing the application can be customized - any forms are still open or only one of these three types or only the first three instances (which requires a link to the first three instances, possibly in a List<Form> ).

Re: global event to create each form - this looks promising.

A similar example is here .

+16
source

I found my solution by slightly changing the answer of Zev Spitz.

Note. This version runs only the first form listed in the Forms list.

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; class MyApplicationContext : ApplicationContext { public List<Form> Forms = new List<Form>() { new Form1() }; private List<Form> FormCollectionToList(FormCollection fc) { List<Form> ff = new List<Form>(); foreach (Form f in fc) { ff.Add(f); } return ff; } private void onFormClosed(object sender, EventArgs e) { Forms = FormCollectionToList(Application.OpenForms); if (Forms.Count == 0) { ExitThread(); } foreach (var form in Forms) { form.FormClosed -= onFormClosed; form.FormClosed += onFormClosed; } } public MyApplicationContext() { if (Forms.Count == 0) { Process.GetCurrentProcess().Kill(); } Forms[0].FormClosed += onFormClosed; Forms[0].Show(); } } 

And this version launches all of them.

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; class MyApplicationContext : ApplicationContext { public List<Form> Forms = new List<Form>() { new Form1() }; private List<Form> FormCollectionToList(FormCollection fc) { List<Form> ff = new List<Form>(); foreach (Form f in fc) { ff.Add(f); } return ff; } private void onFormClosed(object sender, EventArgs e) { Forms = FormCollectionToList(Application.OpenForms); if (Forms.Count == 0) { ExitThread(); } foreach (var form in Forms) { form.FormClosed -= onFormClosed; form.FormClosed += onFormClosed; } } public MyApplicationContext() { if (Forms.Count == 0) { Process.GetCurrentProcess().Kill(); } foreach (var form in Forms) { form.FormClosed += onFormClosed; form.Show(); } } } 
-1
source

also you can use 3 tabs with one form instead of 3 forms

-2
source

All Articles