Wyo VB Lecture Notes
Objective #1 - Use multiple forms in a project
- Often, in commercial applications, a splash screen (which is also known as a splash form) is the
first form that shows up when a program is launched. The splash screen presents
the name and version number of the program as well as the names of the authors,
etc. It is meant to distract the user while the rest of
the program (video, large graphics, etc.) loads into the memory of the computer. When the user clicks the splash screen or
presses any key, this screen 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. You can add as many forms as you'd like to a VB project.
- The second form will appear in the Solution Explorer window. You can place objects on its interface and type code into this form's Code Window like any other form.
- By default, the project's first and only form is set to initially launch when a program is executed. However, you can change this behavior and make another form such as a splash screen show up initially. To do this, click the Project / NameOfProject Properties... menu command (where NameOfProject is replaced by the name of your VB project.) This window contains properties that affect that whole project rather than just one object or form. Click the Application tab on the left of this Project Properties window. Then, change the entry under the "Startup form" property to the name of the form that wish to open when the program is launched.
- The Show method can be used to display another form on the screen. For example, the statement
Form2.Show()
will cause a form named Form2 to be displayed. You can also set the Visible property of a form to True in order to display it as in
Form2.Visible = True
Normally, you would use one of these statements in a KeyPress or MouseClick method so that it displays when the user
presses any key or clicks anywhere on the splash form as in:
Private Sub Form1_KeyPress(. . .)
Form2.Show()
Me.Hide()
End Sub
or
Private Sub Form1_MouseClick(. . .)
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. You can also hide a form by setting its Visible property to False as in Me.Visible = False
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.
- While you can add as many forms to a VB project as you'd like, only one automatically loads and displays when the application is executed. 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, place the Application.Exit() statement in a form's FormClosing method as in
Private Sub Form1_FormClosing(. . .)
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.
- A module (aka code module) is a special file that can be added to a project as a place to store reusable code. It is wise to type out functions and methods into a code module so they can be used and re-used on multiple forms. As long as you type Public instead of Private on the first line of a method or function, then the method or function can be called and used anywhere within the project. For example
Public Function DiceRoll() As Integer
Return Math.Floor(Rnd() * 6) + 1
End Function
is a function that can be reused on any form since it is Public. This is handy for general utility functions that are used throughout a large product such as a function that returns a random number.
- 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.
- Even though it is a dangerous and considered bad style, you can declare global variables in a code module with statements like
Public gScore As Integer = 0
Since the keyword Public is used to declare gScore instead of Dim, the variable can legally be referenced and used on any form within the project. Therefore it has global scope.
This is dangerous since it can be very tricky to debug programs that have global variables whose values are affected by code in many forms. If multiple programmers are involved in updating a large program that contains global variables, they may not communicate well and accidentally change the values stored in global variables having devastating effects on code written by other programmers.
But sometimes it is convenient for beginner programmers
to use a global variable or two to store a player's score in a game that has many levels for example. The prefix g is
used in global variable names so that they are easy to spot by other programmers. Sometimes we even type a global variable with all uppercase letters as in Public PI As Double = 3.14