You are on page 1of 21

VISUAL BASIC OVERVIEW

CREATING A NEW PROJECT

A NEW VISUAL BASIC PROJECT

FORM WITH SOME TOOLS

Designing a tic-tac-toe program


For our first example, we might create a small tic-tac-toe program using nine buttons in a form When the user clicks a button, we can display an x in the buttons caption If the user clicks another button, we can display an o, and so forth.

(Contd .)
Using the Command Button tool in the Visual Basic toolbox, add a new command button to the main form in our program. 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.

(Cont)

(Cont )
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 33 grid similar to a standard tictac-toe game, give each of the buttons the name Command, and clear their captions. That completes the preliminary design now were ready to write some code.

(Contd )

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

(Contd )

(Contd..)
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()

(Contd..)
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 Dimto indicate that xNow is a Boolean variable, we could declare it this way:
Dim xNow As Boolean

(Contd..)
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

(Contd..)
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

RUNNING A TIC-TAC-TOE PROGRAM

VARIABLES TYPES IN VISUAL BASIC


VARIABLE TYPE BOOLEAN BYTE BYTES OF STORAGE 2 1 RANGE True or False 0 TO 255 -922,337,203,685,477.5808 to 922,337,203,685,477.5807 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59 -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 -1.79769313486232E308 to 4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 N/A -3.402823E38 to -1.401298E-45 for negative values and from

CURRENCY 8 DATE DECIMAL DOUBLE 8 12 8

INTEGER LONG OBJECT SINGLE

2 4 4 4

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.

You might also like