|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Designing The Tic-Tac-Toe Program Using the Command Button tool in the Visual Basic toolbox, add a new command button to the main form in our program now, as shown earlier in Figure 1.2. Next, in the Properties window, change the Name property of this button from Command1 to Command in preparation for setting up a control array, and clear its Caption property so the button appears blank. Next, add a second button to the form, and set its Name property to Command as well. When you do, Visual Basic opens a dialog box that states: You already have a control named Command. Do you want to set up a control array? Click Yes to create a control array, which means we will be able to refer to our controls using an index instead of simply by name. Add a total of nine buttons to the main form in our program, arranged in a 3×3 grid similar to a standard tic-tac-toe game, give each of the buttons the name Command, and clear their captions. That completes the preliminary designnow were ready to write some code. Coding The Tic-Tac-Toe Program In this program, well toggle button captions between x and o. To start coding, double-click any button, opening the code window, as shown in Figure 1.5.
Double-clicking a button creates an event handler subroutine named Command_Click() and opens that subroutine in the code window: Private Sub Command_Click(Index As Integer) End Sub Visual Basic programs like this one are centered around events, and most events occur when the user triggers them. In this case, a Click event is triggered when the user clicks a button, and were passed the buttons index in the control array of buttons as the Index parameter in Command_Click(), as with this line of code from the earlier snippet: Private Sub Command_Click(Index As Integer) When the user clicks a button, we need to know which character to display, and well keep track of that in a form-wide variable named xNow; if xNow is True, we should display an x, if False, an o. To add that form-wide variable, click the (General) entry in the left drop-down list box in the code window, and add this code to the general section of our form: Dim xNow You can indicate the type of a variable when declaring it with Dimto indicate that xNow is a Boolean variable, we could declare it this way: Dim xNow As Boolean (Declaring it without a type makes it a variant, which means it can operate as any type of variable.) The possible variable types and their ranges appear in Table 1.1.
We need to initialize that form-wide variable, xNow, and we do that when the form first loads in the Form_Load() procedure, which is run when the form is first loaded. Open that procedure now by selecting the Form item in the code windows left drop-down list box, or by double-clicking the form itself; here, we just initialize xNow to True:
Private Sub Form_Load()
xNow = True
End Sub
Now we will toggle the clicked buttons caption depending on the current setting of xNow. To reach the clicked button in Command_Click(), we use the control array index passed to us this way:
Private Sub Command_Click(Index As Integer)
If xNow Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
...
End Sub
Finally, we toggle xNow (from True to False or False to True) this way:
Private Sub Command_Click(Index As Integer)
If xNow Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
xNow = Not xNow
End Sub
And thats all we needthe tic-tac-toe program is complete. Run it now, as shown in Figure 1.6, and click a few buttons. The captions toggle between x and o as they should.
Its not a very exciting program as it stands, of course, because it was just designed to give us a look into how Visual Basic projects work. Now well take a closer look at the parts of a project, starting with the one weve just created. The Parts Of A Visual Basic ProjectProjects can become quite advanced in Visual Basic, even containing subprojects of different types. From a programming point of view, however, standard Visual Basic projects usually contain just three types of items: global items, forms, and modules, as outlined in Figure 1.7.
Forms Forms are familiar to all Visual Basic programmers, of coursetheyre the templates you base windows on. Besides standard forms, Visual Basic also supports Multiple Document Interface (MDI) forms, as well as a whole number of predefined forms that well see in the next chapter.
|
|
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. |