Beginning Visual Basic - Project 1a
Procedural (Linear) verses Event-Driven Programming—Hello World
What is Procedural (Linear) Programming
All computer programming of the past was Linear. A program was composed of lines of code that were executed one line after another from top to bottom (lines of code which achieved a specific goal were usually grouped together into what are called Procedures. Never-the-less, these procedures were executed in a Linear way). When the program was run, it executed its code without delay. This type of programming is called Procedural or Linear Programming. Programming in Visual Basic adds a twist to what is historically known as procedural programming with the introduction of Event-Driven Programming.
What is Event-Driven Programming
When you run a Windows program, here’s what you see: A window with Objects (Controls) inside it, such as Command buttons, Text boxes, List Boxes, Options buttons, etc. And what does it do? Nothing—It waits for an event. An event is an action, usually (but not always) initiated by the user, such as clicking on a command button, moving the mouse pointer, pressing a key on the keyboard, etc. After an event occurs, any code inside the appropriate event procedure of the object which received the event is executed. Most objects have Event procedures attached to them, (e.g., Command Buttons come with a Click event-procedure).
Hello World
In a bow to tradition, you will now create a Hello World program. Ever since I can remember, the first program that all beginning programmers write is a Hello World program. What a Hello World program entails is this: You create a simple program that writes the message "Hello, world!" on the screen. Sound simple? It is. As a Procedural application using the old version of Basic for DOS, this program can be as small a one line of code, like so:
Print "Hello, world!"
When the old, linear Basic version of this program is run, the above line of code is executed immediately, the message "Hello, world!" is displayed on the screen, and the program terminates. This same line of code can be used in your Event-driven Visual Basic version of this program as well. But where does this code go? Remember that in an Event-driven programming environment, almost all code must be placed within the event procedures of the controls used in the program. Since the first version of this program will have only one control—a blank Form—you must decide which event procedure of the Form to insert the Print "Hello, world!" command in.
Below is a list of just a few of the Event Procedures that come with a Form. One of these must contain the Print "Hello, world!" code for your program. But which one?
Event procedure name
Event which executes it
Form_Activate The Form becomes the active window Form_Click The user clicks the mouse on the form Form_MouseMove The user moves the mouse pointer over the form Form_Resize The user changes the size of the form Form_DblClick The user double-clicks the mouse on the form
To duplicate the action of the old-time Linear version of this program, you could put the Print "Hello, world!" command into the Form_Activate event procedure. This would execute the print command, when the program is first run (or activated). But that doesn’t really show how the Event-Driven programming of Visual Basic works. Let’s experiment, and see how the Print "Hello, world!" command can be inserted into any of the above event procedures, and still do the job. Follow these steps to see how:
1. Run Visual Basic (this starts a new Project automatically. If you’re already in Visual Basic and want to manually start a new Project, just select New Project under the File menu, and choose Standard EXE as the project type).
a) Press the F7 key, or click the View Code button at the top of the Project Explorer window to pop open the Code Window.
b) Select Form on the Object drop-down menu of the code window (the drop-down list on the top Left of the code window).
c) Pull down the Procedure drop-down menu and scroll it up and down to see all of the event procedures that come with a Form (the drop-down list on the top Right of the code window). Choose the Activate event procedure from the list.
The code for the Form_Activate event procedure should look like this. Blank, except for the procedure’s beginning and ending statements:
Private Sub Form_Activate()
End Sub
Your cursor (the vertical flashing line) should be sitting on the blank line between these two lines. Press the Tab key once to indent the blank line and type this command:
Print "Hello, world!"
Now click the Start button on the toolbar to run the program. What happens? Time to experiment: Remove the Print "Hello, world!" command from the Form_Activate event procedure, and put it in the Form’s Click event procedure (Form_Click). Select the Click event procedure from the drop-down Procedures list at the top right of the Code Window like you selected the Activate event procedure earlier. Run the program again, now click anywhere on the form. What happens? Click on the Form again. What happens? Try running the program after putting the Print "Hello, world!" command into each of the different event procedures listed above, and see what happens (Important Note: Be sure to delete the Print "Hello, world!" code from each Event procedure before trying it in another one, so that the code is only inserted into one event procedure at a time).