Visual Basic (VB) – Basics & Visual Studio IDE

Dr. Bandula Jayatilaka

Analysis of a Simple Windows Application

The following is an analysis of the program I introduced in the last notes and demonstrated in the lab. At this stage, we will focus only on few constructs in the program. As we progress in the semester we will learn about the other items.

The program displayed a window similar to the following (not the same).

There are several objects in the program:

(i)The form where we display the buttons

(ii)The message label

(iii)Buttons

Key words of the language (constructs):

Visual Basic language has a set of defined words. In our program these words are in blue and they are supplied by the Visual Studio. (but we need to know them and we will discuss these later).

Some key words in the program (see program next page)

As

ByVal

Class

End

Handles

Private

Public

Sub

Comments:

A comment starts with a single quotation mark

‘this is a comment

The comments in the program are shown in green.

Events:

Each button click is an event. The events have to be handled by an appropriate action in a method.

Following is the code associated with the form:

PublicClass HelloForm

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Display a Hello World message in English.

MessageLabel.Text = "Hello World"

EndSub

PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Exit the project.

Me.Close()

EndSub

PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

'Display a Hello World message in French.

MessageLabel.Text = "Bonjour tout le monde"

EndSub

PrivateSub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

'Display a Hello World message in Chinese.

MessageLabel.Text = "世界您好"

EndSub

PrivateSub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

'Display a Hello World message in Russian.

MessageLabel.Text = "привет мир"

EndSub

PrivateSub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

'Display a Hello World message in Spanish.

MessageLabel.Text = "Hola Mundo"

Label1.Text = "XXX"

EndSub

PrivateSub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

'Print the form on the Printer

PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview

PrintForm1.Print()

EndSub

PrivateSub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

EndSub

PrivateSub Button8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

'Display a Hello World message in Arabic.

MessageLabel.Text = "مرحبا"

EndSub

EndClass

Program components

Class

The form is associated with a Class named as HelloForm. This is the name I have given to the form. It is Public class and ends with the End Class statement.

Methods

Method starts with the word Sub and ends with End Sub.

A method can be Public or Private

There are methods in the class that handles button click events. The methods start with

Private Sub

They are private to the HelloForm class and the method is entered based on the button clicked.

For example, pressing ‘English’ button causes entry to ‘Button1_Click’method.

Buttons wit Languages

When the event occurs, we want to send a message to the messageLabel object to display the text we want (i.e., Hello World).

We set the value of the MessageLabel object’s Text property to Hello World

The object names is MessageLabel

The property name is Text

We use the dot(.) to indicate that the property Text belongs to the object Message Label

MessageLabel.Text ="Hello World"

Exit Button

We need to close the form when Exit button is clicked.

Closing is done by Close() method. It belongs to the Class it is called from. To indicate it belongs to the same class it is called from we use the word ‘Me’.

Me.Close()

Exploring IDE environment

Properties of objects

An object’s properties can be viewed using the Properties window

At this stage, we have come across two properties:

Name

Text

We can change properties using the Properties window. Enter the new value you want the property to have in the row with the property name.

Toolbox

We used Toolbox in the lab, and we dragged controls from the toolbox to the Form.

Object Browser

We can explore the objects in our program using the Object Browser, which can be made visible using the View menu.

Visual Basic Language

Alphabet

Letters, numbers, and some special characters such as _ “ “ ( ) ‘ + - / *

Visual basic is not case sensitive i.e., it will not distinguish between lowercase and uppercase letters. For example, Name will be same as NAME or name or nAmE

Acceptable words

Keywords – predefined words – see textbook’s page 78 for the list of keywords

Identifiers – these are the names or labels that we give to memory locations (we commonly called these variables or parameters).

Rules for forming identifiers

(i)An identifier can consist only of letters, digits and underscore character (_)

(ii)An identifier cannot start with a digit.

Primitive types

Visual Basic has a set of pre-defined types, which are called primitive types (see page 90 Fig. 3.17).

I have categorized them according to the type of data they typically hold.

They are as follows

Logical types

Boolean

Numeric types

Numeric types vary depending on the range of values they can hold, and whether they are signed or unsigned (starts with letter U). Also they fall into two major categories integer or real types.

Byte

Decimal

Double

Integer

Long

SByte

Short

Single

UInteger

ULong

Ushort

Character

Char

Strings

String

Date

Date

Operators

Visual basic has several operators that enable us to manipulate our data and to make decisions within our programs. There are different categories of operators.

Arithmetic Operators

Some arithmetic operators such as addition, subtraction need two operands and they are called binary operators. Some other operators need only one operand they are called unary operators. The following table shows all the arithmetic operators (cf. page 94 Fig. 3.22).

Operation / Operator / Example expression
Addition / + / d + 9
Subtraction / - / d - 9
Multiplication / * / d * 9
Floating point Division / / / X /y
Integer Division / \ / J \ k
Modulus / Mod / r Mod s
Exponentiation / ^ / p ^ q
Unary Minus / - / - x
Unary Plus / + / + x

We can use parenthesis to group parts of an expression. Use of parenthesis will: (1) make the expression clearer, and (2) specify the order in with the expression will be evaluated

Operator Precedence

An expression might have more than one operator. The order in which an expression uses these operators will affect the result of the expression. The order of the evaluation of operators is defined by the precedence associated with them.

Associativity

In some expressions, we might have operators of the same precedence. In that case, the order of evaluation is determined by its associativity. Associativity can be: (1) left to right (2) right to left.

Left to right – the left operator is taken first and then the right operator

Right to left – the right operator is taken first and then the left operator

Following table shows the precedence of arithmetic operators.

Operation / Operator / Associativity
Exponentiation / ^ / Left to right
Unary minus or plus / -, + / Left to right
Multiplication and division / *, / / Left to right
Integer Division / \ / Left to right
Modulus / Mod / Left to right
Addition and Subtraction / +, - / Left to right

1