Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Windows Forms
 How to: Make a Startup Windows Form...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Windows Forms Programming
How to: Make a Startup Windows Form Invisible

To make the main form of a Windows-based application invisible when the application starts, you must move the application's startup logic into a separate class. You cannot simply set its Visible property to false.

After you have separated the lifetime of the application from the lifetime of the form, you can make forms visible (and invisible), because the application will end when you "close" the class that was used for application startup.

NoteNote:

Because a module is invisible when its code is running, the procedure that follows includes a step for adding a message box to the startup module to simply demonstrate that the application is running.

To set a form to be invisible at its inception

  1. Do one of the following:

    1. In Visual Basic, add a module to your Windows-based application by right-clicking the project and choosing Add Module.

    2. In Visual C#, create a new class.

    3. In Visual C++, open Form1.cpp of your Windows-based application.

      For more information about creating a Windows-based application, see How to: Create a Windows Application Project.

  2. Within the module or class, develop a Main subroutine that can act as the startup object for the project.

    The following code example shows how you might approach this.

    Visual Basic
    Sub Main()
       ' Instantiate a new instance of Form1.
       Dim f1 as New Form1()
       ' Display a messagebox. This shows the application is running, 
       ' yet there is nothing shown to the user. This is the point at 
       ' which you customize your form.
       System.Windows.Forms.MessageBox.Show( _
          "The application is running now, but no forms have been shown.")
       ' Customize the form.
       f1.Text = "Running Form"
       ' Show the instance of the form modally.
       f1.ShowDialog()
    End Sub
    
    
    

    C#
    // All methods must be contained in a class.
    // This class is added to the namespace containing the Form1 class.
    class MainApplication
    {
       public static void Main()
       {
          // Instantiate a new instance of Form1.
          Form1 f1 = new Form1();
          // Display a messagebox. This shows the application 
          // is running, yet there is nothing shown to the user. 
          // This is the point at which you customize your form.
          System.Windows.Forms.MessageBox.Show("The application "
             + "is running now, but no forms have been shown.");
          // Customize the form.
          f1.Text = "Running Form";
          // Show the instance of the form modally.
          f1.ShowDialog();
       }
    }
    
    

    J#
    // You can use this Mian method in the Program class (Program.jsl):
    public static void main(String[] args)
    {
       // Instantiate a new instance of Form1.
       Form1 f1 = new Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System.Windows.Forms.MessageBox.Show("The application "
          + "is running now, but no forms have been shown.");
       // Customize the form.
       f1.set_Text("Running Form");
       // Show the instance of the form modally.
       f1.ShowDialog();
    }
    
    

    Visual C++
    void Main()
    {
       // Instantiate a new instance of Form1.
       Form1^ f1 = gcnew Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System::Windows::Forms::MessageBox::Show(
          "The application is running now, "
          "but no forms have been shown.");
       // Customize the form.
       f1->Text = "Running Form";
       // Show the instance of the form modally.
       f1->ShowDialog();
    }
    
    NoteNote:

    The message box in the previous code example is specified with a fully qualified namespace because the created module, unlike a standard Windows Form, does not import any namespaces by default. For more information about importing namespaces, see References and the Imports Statement (Visual Basic), the using Directive (C# Reference) (Visual C#), or the using Directive (C++) (Visual C++). Application.Run() will start the message pump, which is vital to the behavior of certain applications and can affect form behavior during certain times in the application lifecycle, such as on shutdown. For more information, see Application.Run Method.

  3. Change the startup object for the project to be Sub Main instead of Form1.

    For Visual C#, set the startup object to the name of the class in the previous code example. For more information, see How to: Choose the Startup Form in a Windows Application, as well as How to: Change the Startup Object for an Application (Visual Basic).

    NoteNote:

    Skip this step for Windows-based applications in Visual C++.

  4. Press F5 to run the project.

    When the application runs, the code within Main() executes first while the instance of Form1 lingers, hidden, until the code to show it is run. This enables you to do whatever you like within the instance of Form1 in the background without the user's knowledge.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Doesn't work if waiting for an asynch event to return results      euro   |   Edit   |   Show History

This works fine and dandy. But if in your form you have an async call, and other events are waiting to get fired once the response is received (as in WebBrowser control's DocumentCompleted event), then you're out of luck, because the form goes out of scope as soon as the line of code is processed (without the message box). If you leave the message box code in there, the form will run properly, but it will appear on your screen as soon as you click the mesage box. Without the message box, the form's constructor will run, but if there's code in the constructor that fires off an async call, the form will not stick around for the event to come back with the result.

Any suggestions?

Thanks!

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker