Visual Basic 2010 How to Program
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 1
Visual Studio 2010 is Microsoft’s Integrated
Development Environment (IDE) for creating, running
and debugging programs (also called applications)
written in various .NET programming languages.
(including C#, C++, and Visual Basic).
In this course we focus on developing applications
using Visual Basic programming language.
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2
The first kind of applications you are going to build are
Windows Forms Applications.
A Windows Forms application executes within a Windows
operating system and has a graphical user interface (GUI)—
the visual part of the program with which the user interacts.
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 3
© 1992-2010 by Pearson Education, Inc.
All Rights Reserved. 4
In lab you will learn more
about Forms’ designing
aspects and explore the
IDE’s windows that will
facilitate that (e.g Toolbox,
properties panel and
Solution explorer).
Our lectures will focus on
Form1 learning Visual Basic
programming language that
function behind such
designs.
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 5
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 6
Visual Basic is an event-driven programming language,
meaning that the code is executed in response to events
triggered by the user like clicking the mouse or pressing a
key on the keyboard. Some other events are selecting an
item from a drop-down list, typing some words into a text
box and more
Every control you place on the form has a set of events
associate with it. When you double-click the control on the
form the code window will open and The default event will
appear at the top right side of the code window, The code
appears on the left side is the event procedure associated
with the load event
© 1992-2010 by Pearson Education, Inc. All Rights Reserved. 7
We will write a code that modify a Label’s text
programmatically.
The text displayed on the Form to change when you execute
the program.
This code performs an action (change the label’s text) when
the Form loads that is, when the program executes and
displays the Form.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 8
How did we move from a
plain form to this?
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 9
Starting with a new windows form application
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 10
◦ To change the file name from [Link] to
[Link] do the following:
• Right click the file in the Solution Explorer
• Select Rename
• Type the new file name.
Note: Don’t forget to put “.vb” at the end of the file name when you rename it, or
you will get an error.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 11
A form’s name is what we have changed in the previous slide.
It is also the name we use within the code!
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 12
While form’s text is what appears in its header
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 13
2-set the font size and type that
1-Drag a label from Toolbox
to be appeared in the label
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 14
1-Drag a PictureBox from 2-select the image displayed in
Toolbox the PictureBox
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 15
1-Drag a PictureBox from 2-select the image displayed in
Toolbox the PictureBox
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 16
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 17
Comments
◦ Line 1 of Fig. 3.2 begins with a single-quote character ('),
which indicates that the remainder of the line is a comment.
◦ Comments improve the code’s readability.
◦ Comments can be placed either:
On their own lines ( “full-line comments”; as in lines 1–2)
Or at the end of a line of Visual Basic code (“end-of-line comments”; as
in lines 9 and 10).
◦ The compiler ignores comments.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 18
Classes
◦ Windows Forms applications consist of pieces called classes.
◦ Classes are logical groupings of methods and data that simplify
program organization.
◦ Methods perform tasks and can return information when the
tasks are completed.
◦ Lines 3–10 define a class that represents our Form.
◦ Every Windows Forms application consists of at least one
class.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 19
Keywords
◦ The words Public and Class (line 3) are examples of keywords.
◦ Keywords are words reserved for use by Visual Basic.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 20
Class Names and Identifiers
◦ The name of the Class—ASimpleProgram in line 3—is
an identifier, which is a series of characters consisting of
letters, digits and underscores (_).
◦ Identifiers cannot begin with a digit and cannot contain spaces.
valid identifiers Invalid identifiers
•value1 7Welcome
•Welcome1_ invalid because it begins with
a digit
•xy_coordinate, _total Input field
•grossPay. invalid because it contains a
space
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 21
(ex: PascalCase)
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 22
Visual Basic Is Not Case Sensitive
◦ Visual Basic keywords and identifiers are not case sensitive.
◦ Uppercase and lowercase letters are considered to be identical,
so asimpleprogram and ASimpleProgram are
interpreted as the same identifier.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 23
The Form’s Load Event and Method
ASimpleProgram_Load
◦ GUIs are event driven.
◦ When the user interacts with a GUI component, the
interaction—known as an event—causes the program to
perform a task by “calling” a method.
◦ Common events (user interactions) include clicking a
Button, selecting an item from a menu, closing a window
and moving the mouse.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 24
All GUI controls, including Forms, have events
associated with them.
A method that performs a task in response to an event
is called an event handler,
The process of responding to events is known as event
handling.
Most of a GUI application’s functionality executes
based on events.
Event handling methods are called automatically.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 25
Note About Public and Private
◦ Most classes you’ll define begin with keyword Public
◦ Most event-handling methods begin with the keyword
Private.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 26
A common event for a Form is its Load event, which
occurs just before a Form is displayed on the screen—
typically as a result of executing the program.
Lines 5–9 define the method ASimpleProgram_Load
as the Form’s Load event handler.
When this event is raised (that is, the event occurs), method
ASimpleProgram_Load executes to perform its task—
changing the text in the Label.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 27
At the end of line 6, the clause
Handles [Link]
indicates that method ASimpleProgram_Load is
the one that will be called to handle the Form’s Load
event.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 28
Defining a Method
◦ The keyword Sub (line 5) begins the method declaration
◦ The keywords End Sub (line 9) close the method declaration.
◦ The body of the method declaration appears between the
keywords Sub and End Sub.
◦ The keyword Sub is short for “subroutine”
◦ Methods are also sometimes called procedures.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 29
When this line executes, it changes the Label’s Text
property to the message Visual Basic is fun!.
This updates the text on the Form (Fig. 3.2).
The statement uses the assignment operator (=) to give the
Text property a new value.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 30
The expression [Link] contains two identifiers
separated by the dot separator (.).
◦ The identifier to the right of the dot is the property name
◦ The identifier to the left of the dot is the name of the Label control.
When you add a Label to a Form, the IDE gives it the name
Label1 by default for the first Label you add.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 31
Writing Code and Using IntelliSense
◦ As you begin typing, a small window appears (Fig. 3.6).
◦ This IDE feature, called Intelli-Sense,
lists keywords, class names, members of a class (which include
property and method names)
◦ Tabs (Common and All) in Intelli-Sense
Common : view the most commonly used matches.
All: view all the available matches.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 32
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 33
Compiling and Running the Program
◦ To execute your program :
Select Debug > Start Debugging
Or press F5
Or the toolbar button .
◦ When you run a program, the IDE first compiles it.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 34
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 35
You can also compile the program without running it by:
◦ selecting build > Build ASimpleProgram
This creates a new file with the project’s name and the
.exe file-name extension ([Link]).
The .exe file extension indicates that the file is executable.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 36
◦ When you type a line in an incorrect way a syntax error occur.
• Happens before execution time.
• Detected by the IDE before the execution .
• The IDE places a blue squiggle below the error, example:
• A detailed description of the error is shown in the Error List window.
◦ Runtime error is a logical mistake in the code.
• Happens at the execution time.
• Cannot be detected by the IDE before the execution.
• It interrupts the execution of the program.
• A detailed description of the error is shown in the Error List window.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 37
If the Error List window is not visible in the IDE, select View > Other
Windows > Error List to display it.
In Fig. 3.7, we removed the parenthesis “)” at the end of line 6.
The error contains the text “')' expected.” and specifies that the error is in
column 33 of line 6.
This informs you that a right parenthesis is missing in line 6.
You can double click an error message in the Error List to jump to the line
of code that caused the error.
For some errors (such as this one), the IDE shows the Error Correction
Options drop-down list and allows you to simply click a link to fix the
error.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 38
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 39
Addition Program
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 40
Addition Program
Code view
Design view
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 41
Design view
Form and Controls Properties
• Form:
• Name: Addition
• Text: Addition Calculator
• Lable1:
• Name: number1Lable
• Text: Number1
• textBox1:
• Name: number1TextBox
• Text:
• Lable2:
• Name: number2Lable
• Text: Number2
• textBox2:
• Name:number2TextBox
• Text:
• Lable3:
• Name: resultLable
• Text:
• Button:
• Name: addButton
• Text: Add
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 42
Run Demo
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 43
Variable Declarations and Naming
◦ Lines 8–10 are declarations, which begin with keyword Dim.
◦ The words number1, number2 and total are identifiers for variables
◦ Variables are locations in the computer’s memory where values can be
stored for use by a program.
◦ All variables must be declared before they can be used.
◦ The variables number1, number2 and total are data of type Integer I
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 44
A variable name can be any valid identifier.
Variables of the same type can be declared in separate
statements or they can be declared in one statement separated
by a comma. E.g.
You should choose meaningful variable names to make your
programs “self-documenting” & more readable.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 45
Tips in naming variables
- The “camel case”.
◦ It is a way for naming variables so that The first word in a
variable-name begins with a lowercase letter and every word
after the first one should with an uppercase letter.
Example: firstNumber.
◦ Helps make your programs more readable.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 46
Using Variables to Store the Values
Entered by the User
◦ Line 12: number1 = [Link]
◦ We call this statement an assignment statement because it
assigns a value to a variable.
◦ The expression [Link] gets the value that
the user typed in the TextBox whose name is
number1TextBox.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 47
What if the User Doesn’t Enter an
Integer?
◦ For this program, if the user types a noninteger value, such as "hello,"
a runtime error occurs.
◦ A detailed description of the error is displayed in the Error List window.
◦ The message displayed in Fig. 3.10 appears when you run the
application using Debug > Start Debugging (or press F5).
◦ In this case, you can stop running the program by selecting Debug >
Stop Debugging or typing Ctrl + Alt + Break.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 48
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 49
Suppose the user enters the characters 45.
This input is returned by [Link] and
assigned to number1.
The program places the Integer value 45 into location
number1, as shown in Fig. 3.20.
Whenever a value is placed in a memory location, this value
replaces the value previously stored in that location.
The previous value is lost.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 50
Suppose that the user then enters the characters 72 in
number2TextBox.
Line 13
' get the second number entered number2 =
[Link]
places the Integer value 72 into location number2, and
memory appears, as shown in Fig. 3.21.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 51
Once the program has obtained values for number1 and
number2, it adds these values and places their total into
variable total.
The statement (line 14)
total = number1 + number2 ' add the two numbers
After total is calculated, memory appears, as in Fig. 3.22.
The values of number1 and number2 appear exactly the same
as they did before they were used in the calculation of total.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 52
Programs often perform arithmetic
calculations.
◦ The arithmetic operators are summarized in Fig. 3.23.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 53
the asterisk (*) indicates multiplication, and the keyword Mod
represents the Mod operator (also known as the modulus or
modulo operator)
Most of the arithmetic operators in Fig. 3.23 are binary
operators, because each operates on two operands.
◦ Example: The addition operator: 1+2
Visual Basic also provides unary operators that take only one
operand.
Example: The minus sign: (-2) ,and the plus sign: (+2)
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 54
Visual Basic has two types of division:
Integer division (\): floating-point division (/):
◦ Gives an Integer result. ◦ Gives a Float number result.
◦ Operands are rounded to the nearest ◦ Operands are not rounded before
integer number before performing performing division.
division. ◦ Fractional part in the result is kept.
◦ Any fractional part in the result ◦ Example:
simply is discarded . 7/2 =3.5
◦ Example: 7.1 / 2 = 3.55
• 7\2 =3 7.7 / 2 = 3.85
• 7.1 \ 2 = 7\2 = 3
7.1 is rounded to 7 before the division.
• 7.7 \2 = 8 \2 = 4
7.7 is rounded to 8 before the division.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 55
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 56
Mod Operator
◦ The Mod operator gives the remainder after division.
◦ The expression x Mod y gives the remainder after x is divided
by y.
◦ Example: 7 Mod 4 = 3, and 17 Mod 5 = 2.
◦ You use this operator mostly with Integer operands, but it
also can be used with other types.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 57
Operators in arithmetic expressions are applied in a precise
sequence determined by the rules of operator precedence
(Fig. 3.24), which are similar to those in algebra.
Operators in the same row of the table have the same level of
precedence.
When we say operators are evaluated from left to right, we’re
referring to the operators’ associativity.
If there are multiple operators, each with the same precedence,
the order in which the operators are applied is determined by
the operators’ associativity.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 58
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 59
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 60
Operators in expressions contained within a pair of parentheses
“()” are evaluated before those that are outside the parentheses.
Parentheses can be used to group expressions and change the
order of evaluation to occur in any sequence you desire.
With nested parentheses, the operators contained in the innermost
pair of parentheses are applied first.
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 61
Redundant Parentheses
◦ As in algebra, it is acceptable to place unnecessary parentheses
in an expression to make the expression clearer—these are
called redundant parentheses.
◦ For example, many people might parenthesize the preceding
assignment statement for clarity as
y = (a * x ^ 2) + (b * x) + c
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. 62
© 1992-2011 by Pearson Education, Inc.
All Rights Reserved. 63
(x+y^2)-u (x+((y+2)-u))
1
1
2
2
3
3
Note: Don’t forget that parenthesis () has the highest
priority amongst all the operators & anything inside them
should be executed first.
© 1992-2011 by Pearson Education,
Inc. All Rights Reserved. 64