Recipe 2.8. Setting the Startup to a Sub Main Procedure


Problem

You decide, after all, that you want to run your own startup code and display the main form after doing some initial nonform processing.

Solution

Sample code folder: Chapter 02\SubMainStartup

Add a Main() method to a module in your application, and use that as the startup code. You will need to display forms on your own.

Discussion

Add a module to your project, and then add the Main() method with at least the following code:

 Module Module1    Public Sub Main()       ' ----- Add startup code here, then…       Application.Run(My.Forms.Form1)       ' …passing the startup form as the argument.    End Sub End Module 

Next, mark this Main() method as the startup code for your application, via the Application tab of the Project Properties window. Disable the Windows Forms Application Framework by clearing the "Enable application framework" field. Then set the "Startup form" field on that same tab to "Sub Main."

As discussed in the previous recipe, all applications begin from some shared method named Main(). You can supply your own Main() method, and it doesn't need to be part of a form. Adding it to a module with your own initialization code gives you the most control over the application's startup process.

The Application.Run() method runs the primary message loop for your application, a standard part of all Windows desktop programs. Pass an instance of your startup form as an argument; Visual Basic will display this form and keep the program running until the user closes this form.

Because you must disable the Application Framework to use a custom Main() method, some of the convenience and usability features included with the Framework will not be enabled by default. For instance, you will have to manually display and hide any "splash" form that appears during the initialization phase of your application.

See Also

See Recipe 2.7 for additional discussion about startup procedures in Visual Basic applications.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net