Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 design—now we’re ready to write some code.

Coding The Tic-Tac-Toe Program

In this program, we’ll toggle button captions between “x” and “o”. To start coding, double-click any button, opening the code window, as shown in Figure 1.5.


Figure 1.5  Using the Visual Basic code window.

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 we’re passed the button’s 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 we’ll 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 Dim—to 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.

Table 1.1 Variable types.
Variable Type Bytes Of Storage Range
Boolean 2 True or False
Byte 1 0 to 255
Currency 8 -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Date 8 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59
Decimal 12 -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335
Double 8 -1.79769313486232E308 to 4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Integer 2 -32,768 to 32,767
Long 4 -2,147,483,648 to 2,147,483,647
Object 4 N/A
Single 4 -3.402823E38 to -1.401298E-45 for negative values and from 1.401298E-45 to 3.402823E38 for positive values
String N/A A variable-length string can contain up to approximately 2 billion characters; a fixed-length string can contain 1 to approximately 64K characters
User-defined data type N/A N/A
Variant N/A N/A

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 window’s 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 button’s 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 that’s all we need—the 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.


Figure 1.6  Running the tic-tac-toe program.

It’s 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 we’ll take a closer look at the parts of a project, starting with the one we’ve just created.

The Parts Of A Visual Basic Project

Projects 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.


Figure 1.7  The parts of a Visual Basic project.

Forms

Forms are familiar to all Visual Basic programmers, of course—they’re 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 we’ll see in the next chapter.


Previous Table of Contents Next


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.