Wyo VB Lecture Notes
Objective #1 - Use multiple forms in a project
- Often, in commercial applications, a splash form is the
first form that shows up when a program is launched. The splash form presents
the name and version number of the program as well as the names of the authors,
etc. The splash form can distract the user for a little while the rest of
the program loads into the memory. When the user clicks the splash form or
presses any key, the splash form disappears and the first form of the application
is presented.
- To include a second form in a project you can use the menu command Project/Add Windows Form. The name of the form should start with a capital letter and include the .vb file extension.
- The second form will appear in the Solution Explorer window. You can create a desired interface and type code into this form's Code Window like any other form.
- To have the program show this second form when the user
presses any key on the splash form, you could use the following:
Private Sub Form1_KeyPress(. . .)
Form2.Show()
Me.Hide()
End Sub
The name Form2 is the name of the other form. The Show method displays it on the screen. The Hide method
is being used to make the splash form invisible. The keyword Me is used to designate the current form
whose Code Window you are typing in. In other words, Me is the splash form itself in this example.
- To select which form is the initial startup form in a project, go to the menu command Project/My Project properties... and be sure to set the Startup form as the desired form.
- To ensure that all forms including hidden ones will be closed when someone clicks the X in the upper-right corner of a form, add the method
Private Sub Form2_FormClosed(. . .)
Application.Exit()
End Sub
Otherwise your project could still be running even if no windows are visible. Hiding a form does not clear it out of the computer's memory.
- In order to communicate between two forms, you can refer to objects such as labels and pictureboxes or even module variables on one form from another form by typing the name of the
form and then the dot operator and so on. For example, suppose a picturebox on Form1 is named picEnemy and you want to move it 10 pixels to the right using a controller button on Form2. Then in the
button's Click event on Form2 you would use the line of code
Form1.picEnemy.Left += 10
Objective #2 - Use a code module to store functions and global variables.
- By clicking the Project/Add Module menu command, you can add a code module to your project. For this VB course, you would only need at most one code module in each project. In the
code module, you can declare global variables with statements like
Public gintScore As Integer = 0
The word Public is used so gintScore can legally be referenced and used on any form within the project. Therefore it has global scope.
This is a bit dangerous since it can be very tricky to debug programs that have global variables whose values are affected by code in many forms. But sometimes it is convenient for beginner programmers
to use a global variable to store a player's score in a game that has many levels. The prefix g is
used because gintScore is
a global variable.
- A method or function that is typed out and made "Public" (instead of "Private") in the code
module can also be called from any form within the project. This is handy for general utility functions that are used throughout a large product such as a function that returns a random number.