Saturday, December 26, 2009

Determine entry with Sub Main()

Determining Your Entry Point with Sub Main()


By default, when you start a Visual Basic project, the first form created will be the first form that the project loads. This is adequate if you have a one-form project. What if you have a project with many
forms or a project with no forms at all? For the multiple-form problem, you could "chain" Form_Load() event procedures--have one form's Load event procedure Load another form--as follows:

Private Sub Form_Load()
Load frmAnotherForm
End Sub

This would work adequately for projects with a limited amount of forms, but it's not the best programming practice, particularly if you have projects that require the presentation of many forms.

For projects that have no forms (and there are such projects, particularly in server-side Internet programs that use Visual Basic), there's nothing to load. What do you do for an entry point (or starting
point) for your program? Visual Basic provides a nonform-based entry point for your program--the Sub Main() procedure. Sub Main() is a special procedure reserved by Visual Basic as the startup
procedure for any given project. Sub Main() must be declared in a module, and there can be only one Sub Main() per project.

Set Sub Main() to be the startup point for a project
1. From the Project menu, choose ProjectName Properties.

2. Select Sub Main from the Startup Object drop-down list on the General page of the Project Properties dialog (see Figure 18.5).

3. Click OK.


FIGURE 18.5 You can choose Sub Main() or any form in your project as the startup object.

After you define Sub Main() to be the startup object for your project, you need to create Sub Main in a module. You can use the Add Procedure dialog that you've used to create user-defined procedures,
or you can manually enter the declaration in the General section of your chosen module. Remember, a project can have only one Sub Main(). After you create Sub Main(), you need to fill in some startup code.

Listing 3.8 shows a Sub Main() that displays two forms by using the Show method (lines 4 and 5) and then displays a message box after all the forms are visible (line 8).


LISTING 3.8 18LIST07.TXT--A Simple Sub Main()

01 Sub Main()
02 `Use the Show method to display both
03 `forms upon startup
04 frmMain.Show
05 frmOther.Show
06
07 `Report that all forms are shown
08 MsgBox "Everything shown"
09 End Sub

The code for some versions of Sub Main() can be simple, but some Sub Main() procedures can be complex. Listing 3.9 shows how Sub Main() is used to invoke a comprehensive startup routine that calls other procedures.

LISTING 3.9 18LIST09.TXT--A More Complex Sub Main()

01 Sub Main()
02 `Load the form and let it run the code in the
03 `Form_Load event handler
04 Load frmMain
05 `Intialize the contact list combo box
06 Call InitComboAsDb(frmMain.cboName, frmMain.DataMain)
07 `Fill the appointment list with today's appoinments
08 Call GetDailyAppointments(CDbl(Cdate _
(frmMain.FormDateString())), _
frmMain.lstSchedule, _
frmMain.DataMain, _
gAppointmentDelta%)
09 `Show the main form
10 frmMain.Show
11 `Set the mouse pointer back to an arrow
12 frmMain.MousePointer = 0
13 End Sub

No comments:

Post a Comment