Beginning Visual Basic .NET - Project 1a

Procedural (Linear) verses Event-Driven Programming—Hello World

Project 1a

Before you can begin Project 1a, you need to create a Solution environment where you can save your projects throughout the semester.  Run Visual Basic .NET now.  If you are prompted to open a project, select the Cancel button on the Open Project dialog.  Drop down the File menu and hold your mouse pointer over the New menu item.  Choose Blank Solution from the pop-out menu:

The New Project dialog will appear:

Type your name in the Name textbox, leave all the other settings at their defaults, and click the OK button.  This creates a new folder inside the Visual Studio Projects folder in My Documents, with your name:

        My Documents\Visual Studio Projects\<Your Name>.

Once your new blank solution is created, drop down the File menu and choose New Project on the Add Project pop-out menu:

The Add New Project dialog will appear:

Be sure the Windows Application template is selected in the Templates pane on the right side, then type Project1a in the Name textbox (as shown above).  Now click the OK button.  This creates a new folder inside the \Visual Studio Projects\<Your Name> folder named Project1a:

        My Documents\Visual Studio Projects\<Your Name>\Project1a

Note: When class is over, be sure to follow the instructions at the end of each project that tell you how to copy your project to your floppy diskette so you can take it home with you.

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 Buttons, Text boxes, List Boxes, Radio 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 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., 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 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. A similar line of code can be used in your Event-driven Visual Basic .NET version of this program as well. But where does the 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. To keep things really simple, in this program we will have only one control—a blank Form.   

Click on the View Code button (circled above) which is located at the top of the Solution Explorer Window, to display the code window for Form1:

To access the Event Procedures of Form1, drop down the Class Name list at the top left side of the code window and choose (Form1 Events) as shown above.  Note: Like the foundation of a building, the Form is the container control that all other controls will be placed on to build the application.  

Once (Form1 Events) is selected on the Class Name list, drop down the Method Name list at the top right of the code window and choose the Click event procedure (as shown above).

Your code window should now look similar to the above illustration.   Note that the cursor is inside the Form1_Click event procedure where you can begin typing code.  Type the following line of code inside the Form1_Click event procedure (as shown above):

Debug.WriteLine("Hello World from Form1_Click")

The above line of code uses the WriteLine method the the intrinsic Debug class to write a string to the OutPut window.  The simple Print command from older versions of Basic is no longer supported.

After typing the above line of code in the Form1_Click event procedure, click on the Start button on the toolbar (circled the illustration above) to run the program.  After about 10 seconds or so, when the compilation is complete, you should see a small blank form.  Click once in the middle of the form, wait a couple of seconds and then click again in the middle of the form.  Now take a look in the OutPut window in the lower right corner of the screen behind the form.  Do you see the Hello world from Form1_Click string appear twice (once for each click) as shown in the example illustration below?

Every time you trigger the Form1_Click event procedure by clicking on the form the code in the Form1_Click event procedure is executed and a new Hello world from Form1_Click line should appear.  Click on the form a couple more times and see.  Now stop the program by clicking on the close button in the upper right corner of the small blank form (Not the one in the far upper right corner of the screen which would close Visual Basic .NET):

Use the list below and add the corresponding line of code to each of these event procedures of the form.  To find each event procedure, choose (Form1 Events) from the Class Name drop down list, and then chose the event procedure name from the Method Name list:

Event Procedure

Add this line of Code

Activated Debug.WriteLine("Hello world from Form1_Activated")
MouseMove Debug.WriteLine("Hello world from Form1_MouseMove")
Resize Debug.WriteLine("Hello world from Form1_Resize")
DoubleClick Debug.WriteLine("Hello world from Form1_DoubleClick")

Now test run the program and watch the messages that appear in the Output window as you interact with the form:

  1. The Activated event should be triggered once when the program starts.
  2. The MouseMove event is triggered hundreds of times when you move the mouse just a few centimeters over the form's surface.
  3. The Resize event is triggered when you grab the border of the form and resize it.
  4. When does the code in the DoubleClick event procedure get executed?

Save the project now by clicking the Save All button on the toolbar.   That completes this project.

The following instructions are provided at the end of each project.  You really don't need to save a copy Project 1a to a floppy diskette, as we will not be using this project again.


To copy a Project folder from your Solution on the Hard Drive to a floppy diskette, follow these steps:

  1. Exit Visual Basic .NET and insert the floppy diskette, that you want to copy the Project folder to, into drive A:
  2. Select the My Documents item on the Start Menu to open the My Documents folder.
  3. In the My Documents folder, double-click the Visual Studio Projects folder to open it.
  4. Double-click on your Solution folder to open it (it should have your name).
  5. Open the Project folder that you want to copy, by double-clicking on it.

Deleting the Obj and Bin folders from inside the Project folder before copying it.

  1. Inside the Project folder, delete the Obj and Bin folders--these folders are created automatically when you open a project.  You do not need to copy them, or their contents, to your floppy diskette.
  2. Hit the Backspace key once--or click the Back button on the toolbar.  This moves you from inside the Project folder to back inside your Solution folder.
  3. Right-click on the Project folder and selected: 31/2" Floppy A: on the Send To fly-out menu.  This copies the Project folder to your floppy diskette.