Académique Documents
Professionnel Documents
Culture Documents
College of Engineering
Department of Civil Engineering
Course Title: Visual Basic
Second Stage
Asst. Lecturer: Ali Kadhim Mohammed
First Lecture
Computer Programming
It means the way we facilitate the computer to solve a certain problem and this
implement by writing a program, which it is a series of steps that leads to make the computer
able to translate what we exactly need from it to solve this problem.
To write a program with a clear steps and requirements, there should be what we call
it a Flow Chart before we start making any program. And this flow chart depends on some
fundamentals which we call them Algorithms.
In previous programming language like FORTRAN, MATLAB, PASCAL, BASIC,
COBOL, HTML, etc.
The required steps to write a program were as following:
1- Write the algorithm for it.
2- Draw the flow chart.
3- Write the program.
1- Writing Algorithm
Ex.1: Write algorithm to find the average of degrees for five students.
Solution: the algorithm for this problem as following:
1- Start.
2- Read the degrees of five students.
3- Calculate the average of degrees by this equation:
Av= (D1+D2+D3+D4+D5)/5.
4- Print the result Av.
5- End.
H.W: Write algorithm to finding a number power to 3.
P a g e 1 | 25
2- Flow Chart
Its a number of shapes that are connected together in such pattern to give a simple
explanation to the way that should be implemented by the program to solve the problem. Or
Flow chart is a diagram that uses standard graphical symbols; to illustrate the sequence of
steps required for solving a problem or specific question.
The most commonly used symbols are as follows:
Start
Read x
Print Z, x
End
Ex.3: Draw the flow chart for a program that will compute the average and product of three
numbers.
Solution:
First: Define the problem.
Output: The average and product of three numbers.
Input: The number X, the number Y, and the number Z.
Solution: Average =(X+Y+Z)/3 and, Product= X*Y*Z.
P a g e 3 | 25
Exercises
1- Write the Algorithm, and draw a flow chart to compute the area and the perimeter of
a rectangle ,whose length and width are known , bearing in mind that the equation of
the area is : Area =L*W and that of the Perimeter is: Perimeter =2*(L+W).
2- Write the Algorithm, and draw a flow chart to solving a first degree equation:
y=3x+2.
3- Write the Algorithm, and draw a flow chart for a program that obtains a number from
the user. Determine the number type (even or odd) and print the result.
4- Draw the flow chart to read degree of 50 students and calculate the average of them.
5- Draw the flow chart to find the sum of the even number from 1:100.
P a g e 4 | 25
Programming Languages
In order to communicate with computers, we need to speak a language that computers
can understand. Like verbal languages, programming languages have their own grammar,
punctuation, and vocabulary. Programming languages have gone through many changes and
evolved to be more understandable to humans. Lets look at the history of programming
languages.
P a g e 5 | 25
Table 1-1 shows the ASCII and binary codes for a few characters
065 01000001 A
066 01000010 B
067 01000011 C
068 01000100 D
069 01000101 E
070 01000110 F
071 01000111 G
072 01001000 H
073 01001001 I
074 01001010 J
If we want to see how the word HI is represented in binary and ASCII, it will be
as shown in Table 2-1.
TABLE 1-2 Examples of ASCII and Binary
H I
ASCII 72 73
Binary 01001000 01001001
We can use ASCII codes in our Visual Basic Programs to validate the input.
P a g e 6 | 25
Assembly language is also known as a low-level programming language because like
machine language, it is designed to be used by machines. Like machine language, assembly
language is hardware specific and it varies from CPU to CPU. There is no machine language
that can be used on all processors. A simple example of an assembly language instruction
would be:
LOAD R5 #20
ADD R2 R3 #10
In the first instruction, you load the value 20 into register number 5. In the second
instruction, you add 10 to the value that is stored in register number 3 and store the result in
register number 2. Each instruction will be abbreviated into a three-letter name known as
its mnemonic. For instance, JMP would represent the JUMP instruction.
P a g e 7 | 25
Since the program is already compiled, running the executable program is much faster
than compiling and running the program simultaneously. During compilation, syntax errors
will be identified, and the program will not compile if errors are found. The compiler will
break down your errors into unrecoverable errors and warnings. Some programs can still
execute with less serious warnings. Consider a compiler as your English teacher. When you
submit your essay, your teacher reviews it for spelling, punctuation, and grammar. At the
end your teacher will count the number of errors and assign a grade to you. This is very
similar to what a compiler does. An interpreter translates one instruction at a time, rather
than translating the whole program. Object codes will not be generated when interpreters
are used. As a result, every time you execute this program, it will go through the translation
process again. This will cause it to be slower than programs that use compilers. Because
interpreters translate one instruction at a time, some errors can be identified once the user
hits the enter key. This interaction will help beginners find their errors without waiting until
the entire program is translated. Interpreters are not as efficient as compilers and are slower.
Many versions of BASIC use compilers as well. Lets look at some of the high-level
programming languages:
Programming Language Rankings in August 2014
Aug 2014 Aug 2013 Change Programming Language Ratings Change
1 2 C 16.401% +0.43%
2 1 Java 14.984% -0.99%
3 4 Object-C 9.552% +1.47%
4 3 C++ 4.695% -4.68%
5 7 Basic 3.635% -0.24%
6 6 C# 3.409% -2.71%
7 8 Python 3.121% -0.48%
8 5 PHP 2.864% -3.83%
9 11 Perl 2.218% +0.18%
10 9 Java Scrip 2.172% +0.08%
11 - Visual Basic 2.014% +2.01%
12 13 Visual Basic.Net 1.310% -0.01%
13 10 Ruby 1.242% -0.83%
14 23 F# 1.096% +0.56%
15 18 Pascal 1.044% +0.42%
16 12 TransactSQL 1.043% -0.35%
17 38 Action Script 1.001% +0.69%
18 14 Delphi/Object Pascal 0.915% -0.00%
19 16 Lisp 0.828% +0.08%
20 17 PL/SQL 0.786% +0.03%
P a g e 8 | 25
Second Lecture
Programming in Visual Basic
Visual Basic Language firstly appeared on 1991 and since that time different versions
based on modifications have shown for using. We are going to study Visual Basic version
6.0. Visual Basic Language is considered from Windows Programming Language, as it is
utilized to create programs can work under windows operating system. You have to be very
familiar with Windows operating system. Visual Basic considered as driven event language
which means; the language that works based on dividing the main program into sub
programs and will be executed when an event took place, like pressing one of the buttons
or moving the mouse. Visual Basic programs display a Windows style screen (called a form) with
boxes into which users type (and edit) information and buttons that they click to initiate actions. The
boxes and buttons are referred to as controls. Forms and controls are called objects. Visual Basic is a
tool that allows you to develop Windows (Graphic User Interface - GUI) applications. The
applications have a familiar appearance to the user. Visual Basic implements graphical user
interface that allows the use of graphics for different applications. It provides visual
interactive windows with user, like Dialogue box for (color, font ...), Input box, and Output
box. Also it is able to create menu to simplify user application. So that when you start a
program you have to select which kind of events and what will be the respond for them.
P a g e 10 | 25
Fig. (2-1): Visual Basic New Dialog Box.
There are two other tab. Existing tab lets you open existing Visual Basic projects to
edit or modify, while the recent tab provides you with a list of most recently created or
edited Visual Basic projects. To start a new project, click on the Standard.EXE icon and
then click on the open button. The windows shown in Fig. (2-2) will open. We now explore
the components of this windows one by one. The menu bar is at the very top. Menu bar
contains most of the utilities that are available to you in the Visual Basic environment. Such
as File, Edit, View, etc. Click on each of these items to see the available utilities.
Under the Menu bar is the Toolbar. Toolbar contains some of the most often used
items from the Menu bar in small icons. Move the mouse pointer on each icon and leave it
for a mount and the function of the icon will appear in a small sidebar.
The Toolbox in the Visual Basic environment contains the controls (also refer to as
object) that are most often used in developing applications. Throughout this course you will
learn how to use all these controls.
P a g e 11 | 25
Fig. (2-2): New Project Environment.
P a g e 12 | 25
Table 2-1: Most frequently used controls in the Visual Basic environment
PictureBox You use a picture box to display text or graphics output.
Label You place a label to the left of a text box to tell the user
what type of information to enter into the text box. You
also use labels to display output.
TextBox Holds text that the user can either enter or change or text
generated by application.
ComboBox Combines the features of list box and text box. The user
can either choose an item from the list or enter a value in
the text box.
ListBox Display a list of items from which the user can choose
one.
P a g e 13 | 25
Shape Used to draw a variety of shapes such as a rectangle,
square, rounded square, oval, or circle, etc.
There are three other windows that appear in the Visual Basic environment. They are
Project window, Properties window and the Layout window. The Project window provides
an explorer type view of all the forms and modules in the project, while Properties window
a list of all the properties available for each control with their corresponding value.
Layout window shows the relative location of the forms on the screen. As you change
the location and size of the forms the changes are reflected in the Layout window. Fig. (2-
6) shows Visual Basics Project Window, Properties window and Form Layout window.
You can change the location of the application window (the form) by moving its location in
the Form Layout window. To change of the Form in the Form Layout window simply click
on it (where its written Form1) and drag it to and location in the monitors picture.
Fig. (2-6): Visual Basics Project Window, Properties window and Form Layout window.
P a g e 14 | 25
Form window is the main window in the Visual Basic environment. Shown in Fig.
(2-7), this is the window within which the application interface is developed. You develop
the application interface by placing controls in the Form.
Handles
Procedure
Control (Object)
P a g e 15 | 25
Once the code edit window appears, the control that the code is being written for is
shown in the top left Combo Box and the event procedure is shown in the top right Combo
Box. Visual Basic is an event driven programming language. This simply means that the
applications that are developed using Visual Basic, much like any other windows
applications, will act upon the users actions (event procedures) such as Click, Double Click,
Drag Drop, Mouse Down, etc. Whenever you want the control in your application to
respond to an event, you put the instructions in the appropriate event procedure. Of course
the user does not initiate all the events. Sometimes events are consequence of other events.
For example as you start an application the main Form is loaded in the memory and shows
up on the screen. This is a Form-Load event, which simply means that the Load event
procedure is activate on the Form control. Using the arrow in the left Combo Box will show
all the controls that are used in the application and clicking on the arrow of the right Combo
Box shows all the available procedure for the control that is shown on the left Combo Box.
Control Properties
Every control in a Visual Basic application has certain characteristics called
properties. These properties dictate the way a control looks, size, color, and screen location
amount other things. When you place a control in your application (either by drag and drop
or by double click on it) the property window will display the properties corresponding to
that control. There is a default value for each property. You may change these values to fit
the purposes of your application. Once a control is placed on a Form, Visual Basic
automatically will issue a name for that control. This is the name that the control will be
known by, to the rest of the application. When you write codes for that control you notice
that the name that Visual Basic has given to this control appears in the top left Combo Box
of the code edit window. This name can be changed, just like any other property that appears
in the property window. Fig (2-9) shows a Form that has a Command Button control placed
in it.
Once a control like the Command Button control is placed on the Form, it has Command1
written on it. This is called the Caption. This may become a little confusing since the Name
property of the control that is also Command1. In other word, both the Name and Caption (what
appears in the Command Button control) properties have the same value. To distinguish between
them, the Caption on the Command Button control was changed to Click on me and the Font
property was changed too.
P a g e 16 | 25
Fig. (2-9): Properties of the Command Button.
Another noteworthy item is that when there are more than one control present in the
application during the design time, the properties of the control that is selected will appear
in the Properties window. Note the handles around the Command Button control in Fig. (2-
9), these handles are indicators that Command Button control has been selected.
P a g e 17 | 25
Third Lecture
Additional Properties for the Form and Some Tools of the Project
Before Execution
1- Additional Most Used Properties of the Form of the project:
1-2 BorderStyle: The shape of the frame for the Form window and it has six distinct shapes,
the most important among them are; (0 none the form will be shown without frame and no
cancel, change size, or minimize buttons, 1 fixed single, the form has fixed dimensions and
cannot be changed, 2 sizable.
1-3 ControlBox: In case of True; the three control buttons will be shown on the title bar of
the Form and vice versa when we choose False.
1-4 Drawwidth, Drawstyle and Drawmode: These properties control the drawing
activities on the Form.
1-5 Fillcolor and Fillstyle: Fill color for the filling color of the Form, and the second one
for the outline of the Form.
1-6 Font: To select the type and the required size for writing when Print order is used.
1-7 Forecolor: Select the color of writing when Print order is used or the color of drawing
when draw order is used.
2-1 Cancel Property: This feature enables True, ESC button in the keyboard, meaning,
you may either press the button or ESC, this feature usually used for ending program.
2-2 Default Property: True to enable Enter key from the keyboard to function same as
pressing this Button. This feature usually is utilized when we utilize the button as agree or
yes.
2-3 Style: Change it into (Graphical), this feature will enable showing a picture as a
background for the button.
2-4 Picture: To enable showing a picture background on the button surface. (By browsing
the picture and inserting it).
2-5 DisabledPicture: To select which picture will be shown on the button.
P a g e 18 | 25
2-6 Mouse Pointer: To change the shape of the mouse pointer while moving above the
button.
3-1 Alignment: To select the direction of the text inside it, it has three values; (left, right,
and center).
3-2 Text: this represents the contents of the tool.
3-3 MaxLength: 0 means we set this tool to contain maximum number of letters.
3-4 MultiLine: In case False, means only one line can be written in this field. True, means
that we can write more than one line in this tool.
3.5 Locked: To lock the contents of this tool from editing and this feature can be activated
when we change it into True. In case False, means the contents can be changed.
3.6 PasswordChar: This feature is used to protect the contents of the text from showing,
and this feature usually used to prevent the password characters from showing.
3.7 ScrollBars: To show or hide the scroll bars for this tool, and this tool cant be activated
until you change MultiLine into True.
4-1 BorderStyle: o-None and 1-Fixed Single, to show or hide the edge of the tool
4-2 Backstyle: To select the color of the background of the tool.
4-3 Alignment: This feature is used to show the text of the tool in the desired direction left,
right, or at center.
4-4 Auto Size: To make the width of the tool automatically change with the length of the
Label text.
4-5 ToolTipText: This feature is very essential to show an indication to for what this tool
can be Utilized. And this feature exists in command Button tool, and text tool, etc...
P a g e 19 | 25
Reminder: Double click on each tool of the project window, will how the code window to
us.
EX.4: Let us put one tool and let it to be Command Button in the Form as shown in the fig.
below and let us change the properties of the Form (the backcolor to be yellow).
Visual Programming:
Code Programming:
Private Sub Form_Load()
Form1.Caption = "This is the first program"
Form1.Width = 6000
Form1.Height = 5000
Form1.Top = 0
Form1.Left = 0
Form1.BackColor = vbYellow
Command1.Caption = "click me"
Command1.Width = 2000
Command1.Height = 500
Command1.Top = 2250
Command1.Left = 2000
End Sub
P a g e 20 | 25
Then let us discuss the result.
Conclusion:
1- If we change the properties of the tool from the properties window, it will change
while we are working in the form and before the project is complete.
2- If we edit the properties from the code window, then the change in properties will
appear after the execution for the program. The fixed procedure to change a property
during the execution is as following (Properties of an object are changed in code
with statements of the form):
Some Types of the Common Objects (Controls) and their Procedures (Events):
1- Events are accompanied with Command Button:
1-1 The Event Click:
This event took place when we press the left button of the mouse or Space button in
Keyboard, this event considers the most used event as most of the tools (controls) are using
it and the most one is Command Button.
Ex.5:
1- Lets make a new project by adding one Command Button to the project Form.
P a g e 21 | 25
2- Double click on the tool (Command Button) to open the code window of the tool.
3- Write the following code between Private Sub Command1_Click() line and End
Sub line (MsgBox "The button has been pressed").
4- Note: MsgBox order is used just to show a text messages for the user.
Now: Lets try to make this event with the Form of the project:
1- From the shown code window, we choose Form from the top left list.
2- After that we write the following text between Private Sub Form_Load()line and
End Sub line.
3- Msgbox "This text has been shown when you pressed on the Form of the project".
Code Programming:
Private Sub Form_Load()
MsgBox "This text has been shown when you pressed on the Form of the project"
End Sub
Conclusion:
In first example; we pressed click on the button we added, then the program acted
and have shown us the text message.
In second example; we pressed one click on the Form of the project and the text has
shown to us.
P a g e 22 | 25
1-4 Event KeyUp:
Same as mentioned in keyDown but when you want to lift your finger from the button.
Ex.7: Use TextBox and Command Button to make a program that shows "Welcome to
Civil Engineering Department " and terminates when you press Exit.
Solution:
1- Open a new Visual Basic Project by clicking on the "New Project" on the "File" menu,
and selecting Standard.EXE from the list of icons.
2- Select "Save As..." from the File menu. A dialog box appears with Form1.frm in the
File Name window. Type "Welcome to Civil Engineering Department" in place of Form1
and click "O.K." (Make sure you are in the directory that you want). Another dialog box
appears with Project1.vbp in the File Name window. Type "Welcome to Civil Engineering
Department" in place of Project1 and click "O.K."
3- Develop the interface using the following information.
P a g e 23 | 25
1- Tools
MultiLine True
Alignment Center
2- Visual Programming
3- Code Programming
Private Sub Command1_Click()
Text1.Text = "Welcome to Civil Engineering Department"
End Sub
Private Sub Command2_Click()
End
End Sub
P a g e 24 | 25
4- Execution of the Program
5- Make Project.
H.W:
1- Writing a program in Visual Basic that displays different information about you when
different Command Buttons are clicked. You may start with Name, Last Name, Address,
and Telephone Number. Make sure your program contains a Clear Command Button. The
title of the program is Information of Student.
2- Write a program to input the length, width, and high of wall then compute the number of
bricks in wall, if you know the brick sides is (24*8*12) cm.
P a g e 25 | 25