![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
You can also specify that a Basic module be executed first when the program runs. To do this, select Sub Main from the Startup Object list and then put the code you want executed when the program starts in a procedure named Main in the Basic module. This technique is not commonly used, but it can be valuable when initialization code in the Basic module must be executed before any forms are loaded. Once the initialization is complete, code in the Basic module displays the programs first form. Chameleon Command ButtonsLets take a few minutes to think about the programs eight Command Buttons. Four of them navigate among the records: forward or back one record, and to the first or last record. What other commands might the user need to enter? An Exit button is necessary, of course, and a List button for displaying the alphabetical list. These two buttons will be available at all times. This brings our total, so far, up to six buttons, but there are still more commands to consider. The program will always be in one of two states: browsing and editing records, or entering a new record (indicated by the EnteringNew Boolean variable). If the user is entering a new record, Save and Cancel commands are needed. If the program is in the browsing state, New (to begin entering a new record) and Delete (to delete the current record) commands are needed. This adds up to four commands for only two remaining Command Buttons. Do we need to add two more buttons? No. We can use a programming trick that allows a single Command Button to serve two or more purposes. By changing the buttons Caption property, and by using a flag variable to determine what the program does when the button is clicked, two of the Command Buttons can serve double duty. Heres how it works:
Some of this action goes on in the Click event procedure for the array of Command Buttons. Youll find this code in Listing 19.4. Listing 19.4 The Command Buttons Click event procedure. Private Sub cmdAction_Click(Index As Integer) For the control array of four action buttons. Dim Reply As Integer If EnteringNew Then If were entering a new record, the first two Select Case Index buttons have different meanings. Case 0 Save. Call SaveCurrentRecord EnteringNew = False cmdAction(0).Caption = New cmdAction(1).Caption = Delete Case 1 Cancel. CurrentRecord = OldRecord EnteringNew = False cmdAction(0).Caption = New cmdAction(1).Caption = Delete Call DisplayRecord(CurrentRecord) Case 2 List. frmList.Show Case 3 Exit. Call ByeBye End Select Else If we're not entering a record. Select Case Index Case 0 New. EnteringNew = True Call AddNewAddress Case 1 Delete. Reply = MsgBox(Delete this address?, vbYesNo + vbQuestion) If Reply = vbYes Then txtFName.Text = txtLName.Text = txtAddress.Text = txtCity.Text = txtState.Text = txtZip.Text = Call SaveCurrentRecord End If Case 2 List. frmList.Show Case 3 Exit. Call ByeBye End Select End If End Sub Two items in Listing 19.4 require special mention:
Listing 19.5 The ByeBye procedure ends the program. Public Sub ByeBye() Close the data file and end the program. Close #FileNum Unload frmAddress Unload frmList End Sub
Why not execute the End statement to end the program? We would like to let the user confirm, just in case the Exit button was clicked by accident. End does not permit this, because it terminates the program immediately. In contrast, unloading a form triggers its QueryUnload event procedure. Code in this procedure can query the user to confirm the desire to end the program. The skeleton of this procedure is shown here: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) End Sub The UnloadMode argument specifies how the Unload event was triggered. Table 19.1 shows the possible values. In our case, the UnloadMode argument will have the value one. Because we dont really care why the form is being unloaded, however, we will just ignore this argument. The Cancel argument, on the other hand, is central to the purpose of this procedure. If code in the procedure sets Cancel to any nonzero value, the Unload event is canceled and the program does not terminate. If Cancel is left at its initial zero value, the program terminates. In our program, well display a message box with Yes and No buttons asking if the user really wants to exit, then set the value of Cancel accordingly. This procedure is shown in Listing 19.6. Listing 19.6 The QueryUnload event procedure. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If the user tries to exit, confirm. Dim Reply As Integer Reply = MsgBox(Exit - are you sure?, vbYesNo + vbQuestion) If Reply = vbYes Then Call ByeBye Else Cancel = True Setting Cancel to a non-zero value End If cancels the exit. End Sub
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |