This is because Form1 will be the main form of the application. Specifically, it will be passed to the Application.Run
method, which will create an ApplicationContext
object with Form1 assigned as main form. When the application starts, it checks if the ApplicationContext
has a main form and if so, the Visible
property of that form will be set to true
, which will cause the form to be displayed.
Or, expressed in code, this is Application.Run
:
public static void Run(Form mainForm)
{
ThreadContext.FromCurrent().RunMessageLoop(-1, new ApplicationContext(mainForm));
}
RunMessageLoop
will call another internal function to set up the message loop, and in that function we find the following:
if (this.applicationContext.MainForm != null)
{
this.applicationContext.MainForm.Visible = true;
}
This is what makes Form1 show.
This also gives a hint on how to act to prevent Form1 form showing automatically at startup. All we need to do is to find a way to start the application without having Form1 assigned as main form in the ApplicationContext
:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// create the form, but don't show it
Form1 form = new Form1();
// create an application context, without a main form
ApplicationContext context = new ApplicationContext();
// run the application
Application.Run(context);
}