search c#.net

Chat With Me

คลิ๊กๆๆ...

Thursday, March 12, 2009

Making a Startup Windows Form Invisible (c#.net)

To make the main form of a Windows application invisible when the application starts, you have to 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.

Note 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 simply to demonstrate that the application is running.
To set a form to be invisible at its inception

In C#.net, create a new class.

Within the module or class, develop a Main subroutine that can act as the startup object for the project. For example, the following code shows how you might approach this.

// C#.net
// 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();
}
}
Note The message box in the code above 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, the using Directive (C#.net)
Note 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.
  1. Change the startup object for the project to be Sub Main instead of Form1. For C#.net, set the startup object to be ApplicationName,MainApplication (as per the naming of the class in the code above).

  2. 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 allows you to do whatever you like within the instance of Form1 in the background without the user's knowledge.

0 comments:

Post a Comment

Bejeweled

SNAKE Game - Top 20 Challenge

Learn c#.net

THE PRIMITIVE GAME - คนป่ามหาโหด