Writing Visual Basic Instructions - VISUAL BASIC TUTORIAL

About us

Ads Here

Wednesday, February 23, 2011

Writing Visual Basic Instructions

The final type of window discussed here is the Code window.  To open the Code window for a form, click the Project Explorer and click View Code, or choose View, Code.  This opens the window shown in Figure 2.7.  The object shown is the Form; the procedure is shown as Load().  The title bar reads Form1 because you have not changed the name of the form.

Figure 2.7

This Code window shows the start of a Form_Load() procedure.

The Code window contains the first two coded instructions, which are entered for you.  The first combines the name of the object with the name of the event procedure.  This coded instruction is shown in the following example, in which Load() is a special type of event:

Private Sub Form_Load()

It occurs when a form is first loaded.  However, suppose you want to change the event from Load() to Click().  By selecting Click (click the procedure list box and scroll to Click, as shown in Figure 2.8), the first coded instruction changes to the following, in which Click() is another type of event:

Private Sub Form_click()

It is triggered by a mouse click.

Figure 2.8

The Code window now shows the start of a Form_click() procedure.

The event has now been changed from occurring at the time the form is loaded to the time when the form is clicked, with the user controlling the time of the event with the click of the mouse.

The ending instruction stays the same.  It reads as follows, which means the end of the procedure (sub):

End Sub

Exercise 2.4  Adding a Click() Procedure

In this exercise, you begin a new project.

  1. Choose File, New Project and open a Standard EXE project.  If the last project is still on-screen and has not been saved, you will be asked if you want to save changes to P2-3.vbp? Click No, unless you have already saved it.  Either way will not lead to problems.
  2. When Form1 appears, double-click to open the Code window.
  3. Change the Project name to Project2_4.
  4. Change the procedure name from Form() to Click() in the procedure list box.

    Your code should read as follows:

    Private Sub Form_Click()
    End Sub
  5. Type the following to create the procedure:
    Private Sub Form_Click()
    'This procedure will display a message on the form
    Print "When you click, look at what happens!"
    End Sub
  6. Close the Code window by double-clicking the Control box icon.
  7. Choose Run,Start;click the Start icon on the toolbar; or press F5 to run your procedure. When Form1 appears, place the mouse pointer on the form and click. What happens?
  8. Click the form again. What happens? It makes you want to click forever doesn't it?
  9. Choose Run, End from the menu bar or press the End button on the toolbar to return to the design of Form1.
  10. Save this form and project. You can call the Form F2-4.frm and the project P2-4.vbp if you want to use them in later exercises; however this is not necessary. Continue with the next exercise using the same form.

Exercise 2.5 Adding a KeyPress() Procedure

In this exercise you add a second procedure to a form. Figure 2.9 shows the revised form. This exercise assumes you have completed Exercise 2.4 and are using that form.

Figure 2.9

Adding a KeyPress() procedure to the form places more than one procedure on the same form.

  1. Choose View, Code from the menu bar. The code you wrote for Exercise 2.4 should appear.
  2. Choose KeyPress from the Procedure list box, and observe what happens to your code.
  3. type the following to complete this procedure:
    Private Sub Form_KeyPress (KeyAscii as Integer)
    'This procedure will display a different message on the screen
    Print "    When you press a key, look at what happens!"
    End Sub
  4. Close this procedure (using the Control menu icon) and run the revised program. There are four ways to run your program:
    • Choose Run, Start.
    • Choose Run, Start with Full Compile.
    • Press F5.
    • Press the Start button on the toolbar.

    This time, press F5 to start your program; then as a test, do the following:

    1. Click the form.
    2. Press a key.
    3. Press a second key.
  5. Click the End button on the toolbar to stop your program a different way, or choose Run, End to stop the program.
  6. When you are returned to Form1, double-click the form to open the Code window. What code appears?
  7. Scroll through the list of procedures in the Procedure menu. Coded procedures are displayed in bold.
  8. Save this form and project at this time. You will be asked to work with the form and project in the next several exercises, but it is wise to save your work periodically. You can save your form as F2-5.frm and your project as P2-5.vbp to keep your examples in sequence. Name the project Project2_5 before saving.

No comments:

Post a Comment