views:

226

answers:

3

Hello everyone,

I am confused about how to make a Form visible. When we create a Windows Forms application, the default Form1 is automatically visible, even without explicit call to Show method. But if we want to show another Form and make it visible, we have to make it visible by calling Show.

Any ideas why there is such differences?

I am using VSTS 2008 + C# + .Net 2.0.

thanks in advance, George

+4  A: 

Take a look at the file "Program.cs" that VS generates for you.

static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1()); // and especially this line :)
    }
}
Stormenet
Hi Stormenet, I did not find Show is called from the code you quoted. Any more hints?
George2
The Application.Run will call Show for you.Basically that line is telling that the initial form for your application is Form1. If you want to show another startup form just change that line.
Stormenet
+1  A: 

because form1 is the main form that is called by Application.Run(new form1());

you'll find this code in the program.cs file and you can change to be any form.

Ahmed Said
Ahmed, if I new another Form, and construct it by using Form2 form2 = new Form2(), the form2 never shows. I think the code is the same as what VSTS generates for me in Program.cs, and I do not know why Form2 does not shows?
George2
+2  A: 

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);
}
Fredrik Mörk
Thanks Fredrik, where do you find the if block of code you posted?
George2
I used Red Gate's Reflector: http://www.red-gate.com/products/reflector/
Fredrik Mörk
Cool, what is the differences between Show and set Visible to true?
George2
None, really: all Show is doing is setting Visible = true.
Fredrik Mörk
Good to know Show is the same as Visible = True, do you have any documents to prove it? :-)
George2
@George2: Check using Reflector ;o)
Fredrik Mörk
Hi Fredrik, any free version of reflector? I find on red-gate, it is not free. :-(
George2
Yes it's free. You need to register with your name and email, but it comes at no charge. And I can't recall ever getting an email from them, so don't worry about being spammed.
Fredrik Mörk
Reflector is free: http://reflector.red-gate.com/Download.aspx
John Saunders