|
|
|||
| H o m e | |||
|
|
V B . N E T S u b M a i n ( ) P r o c e d u r e By Michael McIntyre
When your Visual Basic Windows application is executed what does the .NET Runtime use as the starting point in your VB.NET code?
If you create a new VB.NET Windows Forms project in Visual Studio.NET, what does Visual Studio.NET automatically add to your program’s executable file when you compile your program?
Where is the best place to control your application’s Splash form, Log On form, Main form, and application level data and behaviors?
The answer to all of the questions above is: the Sub Main() procedure.
As a Newbie using Visual Studio.NET it is easy to miss this important part of creating a VB.NET Windows Forms program. If you are unaware that Sub Main() is needed and you do not add your own Sub Main() code, Visual Studio magically adds a Sub Main() method to your program’s executable file when you compile your application. You never see it.
This feature that automatically adds a Sub Main() method is nice while you are learning how to write the Hello World! application. But this feature will get in your way when you start writing real applications.
This article will explain how to create your own Sub Main() procedure and how it can be used to control your application from start-up to shut-down.
Sub Main() Procedure Example Code
<STAThread()> _ Shared Sub Main() ' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub
The code above shows a simple Sub Main() procedure that will work when added to Form code. Once the code is added, Visual Studio.NET will recognize that the form has Sub Main() procedure code and will use that code when it compiles the program – instead of automatically adding a Sub Main() procedure.
To test the code:
1. Start a new VB.NET Visual Studio.NET Windows Forms project. 2. Open Form1 in the Code Editor. 3. Copy the code from the article and paste it into Form1 as shown below.
Public Class Form1 Inherits System.Windows.Forms.Form
Windows Form Designer generated code
<STAThread()> _ Shared Sub Main() ' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub
End Class
4. Run the project.
Your Sub Main() code provides the entry point for the application. Form1 will open just as if you had not added the Sub Main() code. The difference is that you are providing the Sub Main() procedure instead of Visual Studio.Net.
You have taken the first step toward mastering the use of the Sub Main() procedure – you are in charge now.
Move Sub Main() to a Class
Next the article will explain how to move the Sub Main() procedure to a class.
Add a new class named AppMgr to the VB.NET project to your project.
Change the accessibility keyword ‘Public’ to ‘Friend’ in the first line of the class statement. This means the AppMgr class can be accessed from anywhere in the application but never from outside the application.
Cut the Sub Main() code from Form1 and paste it into the body of the new AppMgr class statement. The AppMgr class code should now read:
Friend Class AppMgr
<STAThread()> _ Shared Sub Main() ' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub
End Class
The next step is to make the AppMgr class the startup object for the project.
1. Right click the Project in the Visual Studio.NET IDE’s Project Explorer pane and choose Properties from the menu that pops up.
2. In the TreeView on the left side of the dialog that opens click Common Properties then General.
3. In the dialog look for the ComboBox under the label Startup Object: . Use the ComboBox to select AppMgr as the startup object for the project.
Test Your Code
Run the project.
The Sub Main() code in your AppMgr class now provides the entry point for the application. Form1 will open just as it did when Sub Main() was located in Form1. The difference is that the entry point for the application is now Sub Main() in the AppMgr class.
The stage is set. With your AppMgr class in place you can modify it to provide application services to your program.
Use AppMgr to Provide Application Services
Next the article will explain how the AppMgr class can provide application services.
Here is our AppMgr at this point:
Friend Class AppMgr
<STAThread()> _ Shared Sub Main() ' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub
End Class
AppMgr is already providing some application services including:
1. Entry Point
Sub Main run’s the application.
2. Proper Thread Model
<STAThread()> is an attribute. It indicates that the application should join the Single-Threaded Apartment (STA). As a newbie you do not need to try to understand the concepts of attributes and the Single-Threaded Apartment until much later. What you can understand is that unless you include the <STAThread()>_ above the Shared Sub Main() line drag and drop, the clipboard, and other things you will want your application to use WILL NOT WORK.
3. Starts the Application
Application.Run(frm1) begins running a standard application message loop on the current thread, and makes frm1 visible. As long a frm1 is open the application continues to run. Once frm1 is closed the application will end.
Add Application Services to the AppMgr Class
The AppMgr class can handle things that should be done before the application is run such as showing a Splash Form and/or authorizing the user with a LogIn Form.
Add two forms to your Visual Studio.Net VB.NET Windows Forms application.
1. Add a new form named SplashForm.
2. Add a new form named LogInForm. To LogInForm add a new button named LogInButton. Set LogInButton’s Text property to: LogIn. Set LogInButton’s DialogResult property to OK.
Replace all AppMgr class code with the code below:
Friend Class AppMgr
<STAThread()> _ Shared Sub Main()
' Provide Splash Form service. Dim SplashFrm As New SplashForm() SplashFrm.ShowDialog()
' Provide Login service. Dim LoginFrm As New LoginForm() LoginFrm.ShowDialog()
' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub
End Class
Test Your Code
Run the project.
The SplashForm will open first. In a real application you would add a timer to hold the form open for a few seconds and then automatically close it. In this simple example click the close box (X) in the upper right corner to close the SplashForm.
The LoginForm will open next. In a real application you would provide a way for the user to enter a login id and a password, and code to validate the user against a file or database. If authorization is successful the application is run, if not it is never run. In this simple application click the LogIn button which will authorize the login.
Finally, if the LogInForm returns a DialogResult of OK the application will be run. If not, the application is never run.
Note: You can experiment to get a DialogResult that is not OK by clicking the close box (X) in the upper right corner of the LogInForm.
In this article you have learned about Sub Main and how to create a AppMgr class that can provide application services from startup to shut-down.
There are many other services that can be added to AppMgr such as a global exception handler and application form management that keeps a reference to each application form accessible to any code in the application. |
||
|
Copyright © 2001-2003 aZ Software Developers. All rights reserved. |
|||