Vbu 1
Vbu 1
Table 1
Terminolog
y
Definition
Object
A thing like a noun in English. Examples include forms and controls you
place on forms such as buttons, text boxes, and icons.
Property
Method
Like a verb in English these are the actions that objects exhibit.
Examples include methods to Show and Hide forms and methods to Print
and Close forms.
Refer to a method with the notation ObjectName.MethodName example
Me.Close will close the current form.
Event
Events are actions usually triggered by the system user such as clicking a
button; however, events can also be triggered by the actions of objects.
For example, closing a form can trigger an event.
Class
If this screen displays, click the Visual Basic Development Settings option in the
list box and then click the Start Visual Studio button
This is the Start Page for Visual Studio 2008 (Professional Edition).
Click the Project link shown in the figure above to create a new project. This opens the New
Project dialog box shown below.
The first project will be a Visual Basic Project using a Windows Forms
Application template as shown in the figures above.
Enter the project name: Ch01VBUniversity.
Click the OK button it takes several seconds to create the project files from the
template.
The Toolbox
The Toolbox:
Contains controls that are used to build forms.
Can be expanded or collapsed with the Pin/Unpin icon.
Parts of the Toolbox can be expanded/collapsed with the +/- icons.
Used to add controls (tools) to a form by either double-clicking or dragging/dropping
(your option on which to use).
Change the form's FileName property click the FileName in the Solution Explorer
window change the FileName property in the Properties window.
Changing the form's FileName also changes the Name property of the form select
the Form and examine the Name property (the easiest way to select the form is to
single-click the title bar).
Change the Title Bar value of a form by typing a new value into the Text property for
the form.
o Select the Form.
o Click the Alphabetic icon shown in the figure below to list the properties
alphabetically.
o Type the new value VB University Student Information in the Text
property of the Properties window.
Size the form as needed by clicking the white squares around the form and
dragging/dropping.
Set the form's StartPosition property to CenterScreen. This will cause the project
when it runs to display in the center of the computer monitor screen.
Set the form's Font property usually you'll leave this at about an 8.25 point font,
but in class I use a 10 point bold font so everyone in the back of the room can see
the overhead display.
Place a Label control on the form by either double-clicking the Label tool or by
drag/dropping a label to the form.
o Notice that when you select the Toolbox's label control, the mouse pointer on
the form becomes a crosshair showing you where the upper left corner of the
label will be displayed.
o Drag the new label to where you want it in the upper left corner of the form.
o Change the label's Text property to Student name:
o The label also has a Name property the first label is named Label1. This is
a satisfactory name and you will not refer to the label when you later write
programming code so leave it named Label1.
Add a second label control on the form. Set the Text property to Academic major: a
this label is named Label2.
If you accidentally place a control on a form and discover that you don't need the control,
simply delete it.
Run the project. Confirm that the project starts up centered on the console, and the
alignment of controls is professional in appearance.
Name property used to name a control for your reference. For example, we will
name the buttons with a name such as DisplayButton or ExitButton.
o Notice that the name begins with a word that describes the control often you
will use the text on a control such as a button as part of the controls name.
o Next the type of control such as Button or TextBox is added to the name to
create a unique name.
o Text boxes may be named NameTextBox and MajorTextBox, or some other
meaningful name.
o If you're not going to reference a control in the project, you can just use the
default name assigned by VB.
10
The Text property for a text box control displays the actual words typed into
the text box.
Locking Controls
After you finish sizing control on a form, you can lock the controls to prevent accidental
movement of the controls.
Right-click any control and select the Lock Controls option in the context menu that
displays as shown in the figure below.
Right-click again and select Lock Controls to unlock the controls.
You'll notice a small "padlock" icon on the upper left corner of the locked controls.
Program Coding
Programming or writing program code is the means used to cause something to happen
when an event, such as a button click occurs. There are six events to be coded for this
project the first three events are for the three display buttons.
11
o
o
You can also access the code view from the View menu or the View icon in
the Solution Explorer window.
The programmer has typed remarks above the Public Class statement
remarks begin with a single quote mark and display in green print remarks
are for humans to read the computer ignores remarks.
12
Accessing Intellisense
VB's Intellisense feature makes it easier for you to type programming statements.
To access Intellisense simply begin to type the name of an object such as the NameTextBox
control shown in this figure VB will pop up a window that displays possible selections this
makes it easier for you to type code and leads to fewer typing errors.
With the Intellisense focus on the NameTextBox, just type a dot (period) and the
Intellisense will next display all of the properties of the NameTextBox type the
letters "Te" (see the figure below) and Intellisense will search and find the Text
property and set the focus to that property for this text box control.
13
14
Printing a Form
Visual Basic includes a PrintForm component this is not a visible control on the form when
added to a form rather, it displays in the component tray of the project.
Return to the forms Design View (the Toolbox does not display in code view).
As shown in this figure, locate the PrintForm component in the Visual Basic
PowerPacks section of the Toolbox.
Double-click the PrintForm component you will see it display in the component tray
of the IDE below the form you are designing. It will be named PrintForm1.
Your printing PrintAction options when printing a form are to print to: (1) Print to Printer, (2)
Print to File, and (3) Print to Preview this third option saves paper so we will use it.
Double-click the Print Form button control and then enter remarks and two coding
statements as shown here.
Private Sub PrintButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles PrintButton.Click
'Print the form to a print preview window
'Assign a PrintAction option
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
'Print the form
15
PrintForm1.Print()
End Sub
16
VB has a smart editor that attempts to find and fix most errors.
If VB cannot understand some of your code, it will display a blue squiggly line under
the part of the line; that cannot be interpreted.
In the figure below the blue squiggly line indicates that VB does not understand
what the textName or textMajor objects are.
Most likely, you named these two objects NameTextBox and MajorTextBox when
designing the form, but then used the wrong name when typing code in the sub
procedure.
There are two other types of errors that we will study in later chapters:
Run-Time error an error or exception that occurs while running a program.
Logic error a program error that causes the display of erroneous output.
A Clean Compile
After you correct errors in your code, access the Build menu and select the Rebuild option
to recompile the program into a new MSIL version.
Compiling means to convert the program from human-readable form to machine-readable
form all referenced classes and components are combined by VB into an executable (.exe)
file.
17
Design Time when you are designing a project using the IDE, you are in what is
termed Design Time.
Run Time when you execute or run a project, you are in Run Time.
Break Time occurs whenever VB encounters some type of processing error that
prevents program execution from continuing. We will cover error-handling
throughout your studies of VB.
VB Help
MSDN
The Microsoft Developer Network (MSDN) library contains books and technical articles to
help you answer questions that will arise as you learn the VB language. This must be
installed on your computer in order for it to be available from the Help menu within VB.
MSDN can be access from a hard drive, network drive, or the Web (requires an
Internet connection).
Hard drive installation is fastest and most convenient.
Web access is from http://msdn.microsoft.com.
Solution
'VBUniversity
'Illustrates displaying information to read-only TextBox
'controls by clicking Button controls
Public Class StudentInfo
Private Sub Display1Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Display1Button.Click
'Display information for the first student
NameTextBox.Text = "Douglas Oppong"
MajorTextBox.Text = "Computer Management & Information
Systems"
End Sub
Private Sub Display2Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Display2Button.Click
'Display information for the second student
NameTextBox.Text = "John Mark"
MajorTextBox.Text = "Computer Management & Information
Systems"
End Sub
18
19