Writing a Procedure to initialize the Form

1. In the Project Explorer window, double-click the InfoSurvey form.

2. Double-click the background of the form to open the code window for the active form.

When you double-click the form or a control, the Code window automatically opens with the Click event for the control that you clicked and is ready for editing.

In the procedure definition (Figure 10-15), Visual Basic automatically adds the keyword Private before the Sub keyword. Private procedures can be called only from the current form module. In other words, a procedure that is located in another module of the current project cannot call this particular (Private) procedure.

At the top of the Code window, there are two combo boxes. The combo box on the left displays the names of all form objects. The combo box on the right shows the events recognized by the selected form object.

3. Click the down arrow in the procedure box on the right, and select the Initialize event. Visual Basic displays the UserForm_Initialize procedure in the Code window:

Private Sub UserForm_Initialize() End Sub

4. Type the form's initial settings between the Private Sub and End Sub keywords. The complete UserForm_Initialize procedure is shown below:

Private Sub UserForm_Initialize() 'select the Hardware option optHard.Value = True

'turn off the Software option and all the check boxes optSoft.Value = False chkIBM.Value = False chkNote.Value = False chkMac.Value = False

'display a zero in the text box txtPercent.Value = 0

'call the procedure to populate the list box with

'hardware options Call ListHardware

'populate the combo box With Me.cboxWhereUsed .Addltem "home" .Addltem "work" .Addltem "school" .Addltem "work/home" .Addltem "home/school" .Addltem "work/home/school" End With

'select the first element in the combo box Me.cboxWhereUsed.Listlndex = 0

'select the first element in the list box Me.lboxSystems.Listlndex = 0

'load a picture file for the Hardware option Me.piclmage.Picture = LoadPicture("C:\cd.bmp") End Sub

To simplify the event procedure code, you can use the Me keyword instead of the actual form name. For example, instead of using the statement:

InfoSurvey.cboxWhereUsed.Listlndex = 0

you can save time typing by using the following statement:

Me.cboxWhereUsed.Listlndex = 0

This technique is especially useful when the form name is long. Notice also that the first element of the list box has the index number zero (0). Therefore, if you'd like to select the second item in the list, you must set the ListIndex property to 1.

The procedure ends with loading a picture into the image control. Make sure that the specified graphics file can be located in the said folder. If you don't have this file, enter the complete path of a valid picture file that you want to display.

The UserForm_Initialize procedure calls the outside procedure (ListHardware) to populate its list box control with the hardware items.

5. Activate the ShowSurvey module and enter the code of the

ListHardware procedure, as shown below:

Sub ListHardware

With InfoSurvey.lboxSystems

.Addltem "CD-ROM Drive"

.Addltem "Printer"

.Addltem "Fax"

.Addltem "Network"

.Addltem "Joystick"

.Addltem "Sound Card"

.AddItem "Graphics Card" .AddItem "Modem" .AddItem "Monitor" .AddItem "Mouse" .AddItem "Zip Drive" .AddItem "Scanner" End With End Sub

Now that you've prepared the UserForm_Initialize procedure and the ListHardware procedure, you can run the form to see the results. 6. Launch the form from the Survey button in the Info Survey worksheet.

After the form is displayed, the user can select appropriate options or click the Cancel button. When the user clicks the Software option button, the list box below should display different items. At the same time, the image control should load a different picture. The next section explains how you can program these events.

0 0

Post a comment