Anda di halaman 1dari 25

University of Misan

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:

Advantage the flow chart


1- Enabling the programmer to have a full understanding to the problem from the start
to the end.
2- Help to discover the errors that might occur in the program especially the logic errors.
3- Enabling the programmer from adding any modifications to the program in such fast
way.
4- Helping the programmer to follow up the parts and details of long programs.
There are simple flow charts that give simple and general view to the program. On the
other hand there are flow charts that contain most of the details of the program for each step
of the program.
Ex.2: Draw the flow chart to find the value of Z from the following Equations, since x value
is known:
Z= x+5 x<0 (1)
Z= cos(x) +4 x=0 (2)
Z= SQR(x) x> 0 (3)
P a g e 2 | 25
Solution: the algorithm for this problem as following:
1- Start.
2- If x less than zero, go to step 3, if x equal to zero, go to step 4, and if x is larger
than zero then go to step 5.
3- Calculate Z from equation No.1 and then go to step 6.
4- Calculate Z from equation No.2 and then go to step 6.
5- Calculate Z from equation No.3 and then go to step 6.
6- Print value of Z, x.
7- End.

Start

Read x

Less than zero Larger than zero


X=0

Z=x+5 Z=cos(x) + 4 Z=SQR(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.

3- Write the Program


After you wrote the algorithm for your problem and draw the flow chart for it, now you start
writing these steps in sentences that the computer can understand and deal with them and
these sentences vary from programming language to another, for example if you want to
print a value in BASIC language you should write ((Print)) whereas; in FORTRAN
language you should write ((Write)) or also ((Print)).

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.

1- Low-Level Programming Language


Machine language is the only language computers can understand. The instruction is
given to computers in binary digits, or a combination of 0s and 1s. Bi in Latin means two,
referring to two states of ON and OFF that 0 and 1 represent. Machine language is the
lowest-level language and is understood by machines only. It would be very hard for a
human to program in binary codes, if not impossible. The smallest units of data is called
bits, which stands for binary digits. Machine language is hardware specific, and each
processor needs its own machine language. The code also contains the instructions to load,
store, move, halt, and other functions needed to perform arithmetic operations. The lack of
standards among machine languages used by different computer manufactures prevented
computers from exchanging information. Later, in the 1960s, ASCII codes were developed
to standardize machine language codes to allow computers designed by different
manufactures to communicate with each other and exchange data. ASCII, which stands for
American Standard Code for Information Interchange, was the product of this effort.
The ASCII character set represents 128 characters that are found on most standard
keyboards. These 128 characters consist of numbers, letters, and special characters, such as
commas, parentheses, and dollar signs. This character set ranges from 0 to 127. This is
known as a 7-bit character set that can represent the first 128 characters. Since most
computers have additional special character sets beyond the first 128 characters, the 8-bit
or byte ASCII character set known as Extended ASCII was developed later on. Although
ASCII codes provided some standards for the exchange of information between computers,
the codes remained semi-standard.

P a g e 5 | 25
Table 1-1 shows the ASCII and binary codes for a few characters

ASCII Binary Character

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.

2- Second Generation of Low-Level Programming Language


As stated above, machine language is designed for a machine and is not readable by humans,
but humans are the ones who must write instructions for the computers. To make it possible
for humans to read and write computer instructions, assembly language was developed.
Assembly language uses a translator called an assembler to translate programmers
instructions to machine language. The structure of assembly language is very similar to
machine language. However, programmers can use symbols instead of the raw numbers,
which makes programming easier.

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.

3- High-Level Programming Languages


Although assembly language is easier to use than machine language, it is still hard to
learn. As we learned, computers can understand only machine language, and we know
humans cannot use machine language directly to comfortably instruct the computer to
perform tasks. As a result, the high-level programming languages were developed. The
grammar (syntax) of some of the high-level programming languages is close to English, so
programmers can learn and use them faster and more easily. Under the category of high-
level programming languages, you will find many programming languages that were
developed to solve specific problems. Unlike machine language and assembly language,
high-level programming languages are designed to interact with humans, not machines. The
high-level programming languages need to be translated into machine language in order to
be understood by computers, which can be accomplished by using compilers and
interpreters.

Compilers and Interpreters


A compiler is a computer program that translates the entire program into object codes. You
can execute this object code without the need to compile it again. Most executable programs
that you receive and execute are object codes, and you do not need the source code to run
these applications. Once they are compiled, they will run as many times as you need. If you
need to modify the program, you can go back to your source codes, make the changes,
recompile it, and generate a new object code (executable program).

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.

The Concept of Event Driven Programming


Event: It is what the user makes as actions on the program, like pressing a button or
selecting item from a list, or moving mouse, or clicking mouse button. When events
occurred, windows take this event to the executed program and giving it a message about
the nature of this event that took place on this program, so that the program will analyze this
message and take the right action for this event after the program complete, the control came
back to the operating system. It is not necessary that the program has to respond to any
event, for example moving the mouse on the screen of the program is considered as an event,
but that does not mean the program has to respond to that event, unless the program is
drawing program. Event driving Programming leads to divide the program into many parts
and each one of them will respond to a certain event, and we start making the code for each
part of the program. Visual Basic make this task easy to us, as it is automatically dividing
the program into parts and each part called action (sub routine) and this action is controlling
in a certain action for a certain tool.
P a g e 9 | 25
To Run & Exit from Visual Basic on user computer
Start>programs>Microsoft Visual Studio 6.0>Microsoft Visual Basic 6.0.
To exit from Visual Basic and return to Windows is like exit from most Windows
applications. There are three ways to close the Visual Basic as stated below.
1- Click on close button icon that appears in the upper-left corner of the screen.
2- Press Alt+F4
3- Select File >Exit.

The Importance of Visual Basic Program


Languages like Basic and Pascal depend on variables and procedures to build the
applications .This is why it is called procedural languages. The new approach is called
object programming for visual programs like Visual Basic and Visual C++ and others. In
this programming approach everything (form, command buttons, controls) is an object.
The reasons for implementing Visual Basic program are listed as follows:
1- It uses integrated development environment (IDE) which is easier for the user to
minimize code writing.
2- All visual programs follow the same concepts, therefore the user will become more
familiar with visual approach for other visual languages.
3- It provides Input box and Output box as an interactive windows with user.
4- It is able to connect to Internet, and to call Explorer.

Visual Basic Environment


In this lecture you will explore the Visual Basic environment. A unique programming
environment that at the beginning can be a bit intimidating, but once you have completed
this topic, you will find that it is actually a rich and user friendly environment. Lets use
start with the first dialog box that you will encounter once you start Visual Basic. Fig. (2-1)
shows the Visual Basics new project dialog box. Using this dialog box you will initiate
your first project in Visual Basic. As you can this dialog box has three tabs. The new tab
lets you initiate a new application. In this course you will mostly create Standard.EXE (EXE
means executable program) applications, i.e. standalone windows programs.
Visual Basic also allows you to create other applications such as ActiveX control and
ActiveX DLL (Dynamic Link Libraries sharable subprograms used by other programs) or
other applications as viewed in this dialog box.

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.

Fig. (2-3): Visual Basic Environment Menu bar.

Fig. (2-4): Visual Basic Environment Toolbars.

Fig. (2-5): Visual Basic Environment Toolbox.


In the following table the controls in the Toolbox will be introduced one at a time. In
the following lectures these controls will be used in different applications and their
properties will be explored. Pointer it does not draw a control. Pointer lets you select, resize
or move a control already on a graphics and print methods. Label displays text that cant be
changed, like caption under a graphic. It can be changed by program codes.

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.

Frame Allow you to create a visual or functional grouping for


controls. Draw the frame, and then draw controls inside
the frame to form a group.
CommandButton Creates a button the user can choose (click) to carry out a
sequence of instructions.

CheckBox To choose between yes no, true false, and include


exclude or multiple choice when more than one item may
be chosen.
OptionButton This control is used to select one item from a group of
items.

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.

HScrollBar Provides a graphical tool for moving through lists or


selecting data ranges.

VScrollBar Provides a graphical tool for moving through lists or


selecting data ranges.

Timer Used to activate a specific event at set interval. This


control is invisible at run time.

DriveListBox Display valid disk drives at run time.

DirListBox Display directories and path at run time.

FileListBox Displays a list of files at run time.

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.

Line Used to draw a variety of line styles on your form at


design time.

Image Displays a graphical image from a bitmap, icon, metafile


as well as JPG or GIF files. It is decorative and uses fewer
resources than a PictureBox.
Data Provides access to data in databases through bound
controls on your form.

OLE Allow you to link and embed objects from other


applications in your visual basic application.

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

Fig. (2-7): Visual Basics Form window.


You can change the size of the Form by clicking on the handles and dragging them.
Once the controls are placed in this window and their properties are assigned in the
Properties window it is time to attach codes to each control as needed. Attaching code to
the control means writing the sequence of instructions that pertains to each control. In order
to write the code for each control you most first bring up the code editing window for that
control. You can do that by double clicking on the control. If you wish to attach code to a
Form, you should double click on the Form itself.

Procedure
Control (Object)

Fig. (2-8): Visual Basics Code Editing Window.

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-1 Appearance: 0 as flat, 1 as 3d.

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- Properties of Command Button Tool:

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- Properties of Text Tool:

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- Properties of Label Tool:

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...

Properties of Tools after Execution of The Program:


In previous lecture, we explained the procedure of changing the properties of tools
before the program is complete. (I e. while we start putting the tools in the project form). In
this topic we will illustrate changing the tools properties after we execute the program and
this thing can be achieved through the code window of the tool.

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):

Object (Control) Name. Property Name=New Value


txtBox.Font.Size = 14 & txtBox.Font.Bold = True
Events and the Code Window:
As we mentioned previously, that Visual Basic is Event Driven Programming
language. If we see the code window in Visual Basic, it divides the code into small
procedures based on the events and this procedure will take place when the event or action
occurred.

Control (Object) Procedure


Or
Event

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.

1-2 The Event DblClick:


We can try it in Label tool by pressing double click on the tool to execute something.
Ex.6: Lets insert Label tool in the Form, then we enter into its Code window and then we
choose double click from the top right list. Then we write end in the code between Private
Sub Label1_DblClick () item and End Sub item. Then run the program and see.
Conclusion:
We can see by pressing only one click on the Label tool the program did not end,
until we pressed double click on it.

1-3 Event KeyDown:


This procedure is applied when we want to press one of the buttons (Keyboard)
constantly for a while and before we lift our finger from it.

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.

2- Events are accompanied with the Form of the Project:


2-1 Load:
Once you choose it from the top right list in the Code window, its action will be
conducted immediately after you run our program. Normally it is utilized for welcoming
messages.

2-2 Event KeyPress:


Always with this event, the private sub will have keyAscii, which it means the
number of the KeyPress button in the table of ASCII.

3- Events are accompanied with Text Tool:


3-1 Change.
4- Events are accompanied with Label Tool:
4-1 Click.
4-2 DblClick.
Both of them have been explained in the events that are accompanied with Command
button.

The three steps to creating a Visual Basic program are as follows:


1- Create the interface; that is, generate, position, and size the objects.
2- Set properties; that is, set relevant properties for the objects.
3- Write the code that executes when the events occur.

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

Object Property Value

Form1 Caption Welcome to Civil Engineering Department

TextBox Name Text1

MultiLine True

Alignment Center

Text Delete the Text1

CommandButton1 Caption Welcome

Font Times New Roman

Font Style Italic, 16

CommandButton2 Caption Exit

Font Times New Roman

Font Style Italic, 16

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

Anda mungkin juga menyukai