Concepts of Computing, Section II

How to build and run a Visual Basic program

Here are some rough notes about using VB.

Back to course page.


Create and save a new program

  1. Start VB.

    Find Visual Basic on the Windows Start menu and start it.

    The large VB window appears with a New Project dialog box (if there is no dialog box, bring it up by File > New Project).

    Select Standard Exe. The VB window shows Project1 with an empty Form1.

  2. Name the project.

    Select Project > Project1 Properties. In the Project Properties dialog box, change Project1 to your project name, say Counter.

    The title bar on the VB window changes to match.

  3. Name the form and set the form caption.

    The Properties window at the right of the VB window shows the properties for the form. The first property at the top of the list is Name. Change the name to, say, frmCounter.

    The title bar in the Form window changes to match.

    Still in the Properties window, scroll down to Caption. Change the caption to the same name as the project, say Counter.

    The title bar on the form itself changes to match.

  4. Save the form (you should always name and save the form before you save the project).

    Select File > Save frmCounter As .... The Save Form As dialog box appears. The Save in: textbox shows the name of the folder where the form will be saved. You should create a new folder for this project. Navigate to the folder where you want to keep all your VB projects and click on the new folder icon (a picture of a folder with a little highlight). After you create the new folder, select that folder. Its name should appear in the Save in: box. The form file name frmCounter.frm should appear in the File name: box (if not, type in the correct name). Click on Save.

    The form is saved.

  5. Save the project (be sure to name and save the form before you save the project).

    Select File > Save Project As .... The Save Project As dialog box appears. The Save in: textbox should show the correct folder (the one you just created for the form). The File name: box should show the correct name (Counter.vbp in this example). Click on Save.

    The project is saved.

After you name and save the form and the project, it is a good idea to exit and then restart VB to make sure you can restore your project --- before you invest a lot of work in it.

Restore a saved program

  1. Start VB.

    Find Visual Basic on the Windows Start menu and start it.

    The large VB window appears with a New Project dialog box. This time choose the Existing tab (if there is no dialog box, bring it up by File > Open Project).

    Navigate to the folder you created earlier. It should contain a .vbp file for the project you created earlier (Counter.vbp in this example). Select that file.

    Alternatively, instead of using the Start menu, you can navigate to the project folder using My Computer or Windows Explorer. Then double-clicking on the .vbp file or the .frm file starts VB and opens that project.

    The restored project appears in the VB window. The project name appears in the title bar.

  2. Open the form.

    An icon for a folder of forms appears in the Project window on the right side of the VB window. Open this folder and select the form you saved. Click on the View Code and View Object icons to display the code or form layout windows.

    Now you are ready to add controls and code to the form. At any time, you can use File > Save frmCounter and File > Save Project to save your changes to the form and the project without going through the Save As ... dialogs.

Write a program based on an earlier program

  1. Restore a saved VB program, as described in How to Restore ... above.

  2. It is a good idea to change the project name and the form name, as described in How to Create ... above.

  3. Make changes in the program.

  4. Save the form and the project in a new folder, using Save As ... (not Save) as described in How to Create ... above.

    Make sure you save the changed program in a different folder. This is necessary to ensure that the earlier program is preserved. Be sure to save the form first.

Add controls to the form

Repeat for each control:

  1. Choose the control.

    In the Toolbox window at the left, point at a control icon. The name of the control (for example, Label) will appear.

    Double click on the control icon.

    The control appears in the middle of the form layout window.

  2. Position and size the control.

    In the Form Layout window, point at the center of the control. Drag control to the position you want (hold down the left mouse button while you move the mouse).

    To size the control, point at one of the little grab boxes at the sides and corners of the control. Drag the side (or corner) to make the size you want.

It is best to set the properties for each control right after you select it and position it.

Set control properties

For each control:
  1. Use the Properties window at the right.

    Select the control from the drop-down menu at the top of the Properties window. Or, click on the control in the form layout window.

    A table showing all the control properties appears, with property names in the left column and property values in the right.

  2. The first property is Name. Type in a meaningful name, using the naming conventions: frm... for forms, cmd... for command buttons, lbl... for labels, tmr... for timers etc.

  3. Set the other properties. For command buttons, set alignment, caption and font. For labels, set alignment, caption, font, and (often) background color. For timers, set interval. And so on.

Write code for event subroutines

For each pertinent control and event:

Use the Code window. Find the relevant event subroutine.

For controller Ctlr and event Event, the subroutine is named Ctlr_Event, for example cmdEnter_Click.

Double-clicking on the controller in the Form layout window puts the cursor at the pertinent subroutine in the Code window.

Run the program

There are several ways to run your program:

The program window appears, looking much like the form you designed. The controls on the window are active. While the program is running, it behaves like any other window on the desktop: you can move it, minimize it, etc.

The indicator on the VB window title bar changes from design mode to run mode. Many items in the VB window disappear during run mode and many menu and toolbar operations not enabled.

Stop the program

There are several ways to stop your program:

The indicator on the VB window title bar changes from run mode to design mode. Items in the VB window reappear, and menu and toolbar operations are enabled again.

Revise the program

In design mode, you can add or remove controls, change control properties and revise code. Your changes will be in effect when you run the program again.

It is not necessary to use the Save operations before your next program run. It is only necessary to save before you exit VB.

Examine the program in Break mode

When the program is running, there are several ways to put VB in break mode:

(Could say more about debugging ...)

Exit

There are several ways to exit VB.

VB will warn you if you have unsaved work.

Back to top


Jon Jacky, jackyj@evergreen.edu