Laboratory Handout Week1
Laboratory Handout Week1
Laboratory Handout Week1
ertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyui
LABORATORY HANDOUT
VISUAL PROGRAMMING
opasdfghjklzxcvbnmqwertyuiopa
ROSALINA, M.KOM
sdfghjklzxcvbnmqwertyuiopasdf
Semester: 2015-3 (2016/03/21 – 2016/09/02)
Faculty of Computing
President University
ghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklz
xcvbnmqwertyuiopasdfghjklzxcv
bnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwe
rtyuiopasdfghjklzxcvbnmqwerty
Table of Contents
Learning Outcome..................................................................................................................................... 2
Learning Outcome
Students are able to:
- Develop a visual basic application
- Design the interface for the user
- Start a new project
- Know the toolbox control
- How to change the properties of control using properties windows
- Determine which events the controls on the window should recognize
- Write the event procedure for those events
VB.NET is implemented by Microsoft's .NET framework. Therefore, it has full access to all the libraries in
the .Net Framework. It's also possible to run VB.NET programs on Mono, the open-source alternative to
.NET, not only under Windows, but even Linux or Mac OSX.
In order to work with VB.NET, you need to understand "object" terminology as defined in Table 1.
Table 1
Terminology 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 Objects have properties – like adjectives in English. Properties describe object behaviors.
Examples of properties include Text, Name, BackColor, Font, and Size.
Refer to a property by the notation ObjectName.PropertyName (use the .dot notation) –
example: TotalDueTextBox.Text or AccountLabel.ForeColor.
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 This is a really abstract term – it is a sort of template for an object. For example, all
forms belong to the Form class of object. All buttons belong to the Button class of
object. Classes include definitions for object properties, methods, and associated events.
Each class is assigned an identifying namespace within the .NET Framework Class Library.
Each new object you create is defined based on its class – the new object is called a class
instance.
VB.Net is an event-based programming language. It provides you with both an interface (a window with
controls of your choosing) and events to respond to (user clicked a button...etc).
VB.Net requires a different thought process than C or other procedural programming languages, and
allows you to focus more on accomplishing a task. C requires you to first build code to display an
interface (a screen) and then more code to get input via mouse and keyboard. Once that is out of the
way, you can start programming your application. It is much easier to create a useful program in VB.Net
than it is in C. In the simpler applications, what you generally do is add a control to the form you are
designing. You then select the event to which you will attach your code and then you write the code.
You can also close/open these windows with the View menu.
RESETTING THE DEFAULT IDE LAYOUT
Sometimes you startup VB and the IDE layout is not what you expect.
Windows you want such as Solution Explorer, Toolbox, or Properties may be closed.
The quick way to reset the layout is through the Window menu, Reset Window Layout
submenu option as shown in this figure.
Visual Basic automatically assigns a value to the Name property of each control, for example, Label1,
Label2, Label3, or TextBox1, TextBox2, TextBox3, or Button1, Button2, Button3. However, it is difficult
to remember the difference between Label1 and Label2 so:
if you are going to later refer to the controls, it is best to rename them to a more meaningful
name,
if you are not going to refer to the controls later, then just use the assigned default name such
as Label1.
When you name an object such as a Label or TextBox or Button, you must follow these rules:
An object name can begin with an alphabetic letter or the special “underscore” character.
An object name can include letters, digits, and underscores.
An object name CANNOT include a space or a punctuation mark.
An object name CANNOT be a VB reserved word such as Button, Close, or TextBox.
An object name can contain a VB reserved word – object names such as PrintButton,
CloseButton, NameTextBox, and MajorTextBox are legal names.
Several naming conventions exist within industry – the common ones are the Hungarian naming
convention and the Pascal naming Convention.
Naming conventions are simply guidelines to help other programmers read your code more easily. We
will use the Pascal naming convention. The rules are:
Begin an object name with an uppercase alphabetic character.
Capitalize each word that is part of an object name.
Select object names that are meaningful.
Append the full name of the control class to the end of the name.
Avoid abbreviations unless they are standard abbreviations such as SSN (social security
number).
Examples of valid names: MajorTextBox, ResetButton, MessageLabel, TotalDueTextBox, and
CloseButton.
This table gives examples of both the Camel Casing and Hungarian naming conventions.
Camel Casing and Hungarian Naming Conventions
Control Type Example Camel Casing Hungarian Example Hungarian
and Camel
Casing Control Names Naming Control Names
Naming
Suffix Prefix
TextBox NameTextBox, MajorTextBox txt txtName, txtMajor
Button ShippingButton, ExitButton, btn btnShipping, btnExit, btnReset
ResetButton
Label NameLabel, OutputLabel lbl lblName, lblOutput
Note: Label controls are often not renamed – they are not referred to later in writing computer code so
the default assigned name is unchanged.
End Class
Saving and Running a Project
To save a project, use the File-Save All menu –AVOID using File Save As menu
options – Repeat, DO NOT USE File Save As menu options. You can click the Save
All button shown in the figure below.
This will save all files in the project.
You can specify the location for saving the project. For a location select either:
o My Documents folder
o The Temp folder on drive C: (C:\Temp).
As you have seen already, forms, labels, text boxes, and buttons have numerous properties. We will
focus on learning just a few of these in this chapter.
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.
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 control’s name.
Next the type of control such as Button or TextBox is added to the name to create a
unique name.
Text boxes may be named NameTextBox and MajorTextBox, or some other meaningful
name.
If you're not going to reference a control in the project, you can just use the default
name assigned by VB.
§ Text property – used to display text values on a control.
o The Text property for a form displays the words that are displayed in the form's Title
Bar.
o The Text property of a button displays the words on the button, such as Display or Exit.
Using the ampersand causes a letter on a button to be underlined.
o 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.
Click event for the button named Display1Button, Display2Button, and Display3Button.
Click event for the button named ResetButton.
Click event for the button named PrintButton.
Click event for the button named ExitButton.
You need to master the following coding statements and concepts in this section:
The Handles clause indicates that this sub procedure handles the Click event of the button
named Display1Button.
The sub procedure ends with the line of code End Sub.
Each sub procedure must have a unique name – VB generates a name by combining the name of
the control + an underscore + the name of the event, e.g., Display1Button_Click.
Each sub procedure has two parameters inside of the parentheses that accompany the event
name – these are named sender and e. For now you can ignore these parameters. Later in the
course we will learn to refer to them whenever we need to in order to write more complex
code.
The form named StudentInfo is created as an instance of the general Form object defined in the .NET
Framework class library.
'Project: Ch01VBUniversity
'D. Bock
'Today's date
'Illustrates displaying information to read-only
' textbox controls by clicking buttons.
Click one of the tabs to display either the View Designer or View Code windows.
Click the View Code icon in the Solution Explorer to open the coding window.
Click the View Designer icon in the Solution Explorer to display the form for additional
design layout.
Click the View menu and select either the Code or Designer submenu options.
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.
Now type an equal sign (=) followed by your name inside double-quotes, e.g., "Douglas Bock".
The statement you've typed should look like this inside the sub procedure – VB will provide the
necessary spacing automatically.
This is called an Assignment Statement and is discussed in detail in the next section.
End Sub
Object.Property = Value
Assign a student name to the Text property of the TextBox control named NameTextBox and a
student’s major to the TextBox control named MajorTextBox. The assignment statements to do this
are:
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 Bock"
MajorTextBox.Text = "Computer Management & Information
Systems"
End Sub
Object.Method()
Return to the design view and double-click the Reset Form button control – this will generate a Click
event sub procedure for this button. Type a remark and two statements to clear the contents of the two
TextBox controls as shown here.
Private Sub ResetButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ResetButton.Click
'Reset the form by clearing the TextBox controls
NameTextBox.Clear()
MajorTextBox.Clear()
End Sub
PRINTING A FORM
Visual Basic includes a PrintForm component – this is not a visible control on the form when added to a
form – instead it displays in the component tray of the project.
Return to the form’s 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.
The printing PrintAction options when printing a form are: (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.
If you have not done so already, add Remark statements before the Public Class statement. These
remarks identify the project, you as the programmer, and the date the program was written and must
ALWAYS be included in the project.
'Ch01VBUniversity
'D. Bock
'Today's Date
'Illustrates displaying information to read-only TextBox
'controls by clicking Button controls
PROGRAM ERRORS
From time to time you will make errors when entering program code. VB expects you to follow specific
rules for syntax, punctuation, and spelling.
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.
Referring to an object such as a label or button control by the wrong name – either you forgot to
name the control or you made a mistake when typing the name.
Forgetting to use the syntax: object.property.
Forgetting the dot between the object and property names will cause an error.
Typing an incorrect or invalid property name will also cause an error.
VB lists errors in the View menu -> Error List window that opens when you run your VB project. The
figure shown below displays an error message corresponding to the error that occurs when you don't
type the name of a control correctly.
There are two other types of errors that we will study in later chapters:
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.
DESIGN TIME, RUN TIME, BREAK TIME
There are some additional terms for you to learn about designing and executing a VB program.
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.