Professional Documents
Culture Documents
1- Visual Basic
Visual Basic is a high level programming evolved from the earlier DOS
version called BASIC. BASIC stands for Beginners' All-purpose
Symbolic Instruction Code. The program codes in Visual Basic resemble
the English language. Different software companies produce different
versions of BASIC, such as Microsoft QBASIC, QUICKBASIC,
GWBASIC, and IBM BASICA and so on.
The New Project dialog allows the programmer to choose what type of
Visual Basic program to create. Standard EXE, which is highlighted by
• Recent: opening a project that has been previously loaded into the
Note: to exit from Visual Basic and return to Windows is like exit from
mostWindows applications. There are three ways to close the Visual
Basic asstated below.
2-Press Alt+F4
Figure (2-3) shows The IDE after Standard EXE is selected. The top of
the IDE window (the title bar) displays “Project1-Microsoft Visual Basic
[design]”.The environment consists of various windows when Visual
Basic is started (by default)
Figure (2-3)
Window, Help menus, and specific command such as: Project, Format, or
Debug menus.
The tool appears on form or double click on tool then the tool appears on
form. Table (1) summarizes the toolbox controls.
Control Description
A control that display any picture you choose in your
PictureBox
computer.
A control that display textin the form, you cannot enter text
Label
during running of project.
1-graphical
Style
0-standard
Each standard form has one code form for code. The user can write code
in this code form. This code will be applied at run time.
Statements
End Sub
Textbox:
The most important property of this tool is the text content which is
described in the following:
Backcolor Textno.Backcolor=vbcolor
Forecolor Textno.forecolor=vbcolor
Command:
The most important property of this tool is described in the following:
Caption Commandno.caption="anyname"
Backcolor Commandno.Backcolor=vbcolor
Forecolor Commandno.forecolor=vbcolor
Label:
The most important property of this tool is described in the following:
Caption labelno.caption="anyname"
Backcolor labelno.Backcolor=vbcolor
Forecolor labelno.forecolor=vbcolor
Shape :
The most important property of this tool is described in the following:
Caption shapeno.caption="anyname"
Backcolor shapeno.Backcolor=vbcolor
shapeno.height= shapeno.height-100
Option button:
The most important property of this tool is described in the following:
Caption commandno.caption="anyname"
Backcolor commandno.Backcolor=vbcolor
Forecolor commandno.forecolor=vbcolor
2- Assignproperties to controls
3- Attach code to control To see how this is done, use the steps in the
following procedures to create a simple application for the following
example
Example 1:
Design a form with one text box and two Commands button. Write a code
so when run project and click on command1 (O.k.) replace the word
(Welcome) in text box, and when click on Command2 (Close) terminate
the program and return back to the form interface.
Solution:
your application’s interface. Then you draw the objects that make
up the interface on the forms you create.
1. Adding a text box to the form. Double-click the toolbox’s
textbox to create a text box with sizing handles in the center
of the form.
2. Adding a Command Button1 to the form. Click on button
and draw button1 to form then the button appears on form.
3. Repeat step 2 to add a Command Button2 to the form.
Setting Properties
The next step is to set properties for the objects. The properties window
provides an easy way to set properties for all objects on a form. For the
Example 1, you’ll need to change three property setting. Use the default
setting for all other properties.
Note:
• The Name property identifies a form or control. It’s necessary only for
writing code.
Form1
Name : Form1
Caption : Example1
Command1
Name : Command1
Caption :O.k
Command2
Name : Command2
Caption : Close
Textbox
Name : Text1
Text :
Writing Code:
The code editor window is where you write Visual Basic code for
your application. Code consists of language statements, constants,
and declarations. To open the code window, double-click the form
or control for which you choose to write code, or from the Project
Explorer window, select the name of a form and choose the View
code button.
code:
Private SubCommand1_click ( )
Text1.text=”Welcome”
End Sub
Private SubCommand2_click ( )
End
End Sub
To run the application, choose start from the run menu, or click the start
button on the toolbar , or F5 Click the command button (O.k.) and see the
“Welcome” displayed in the text box. Click the command button (close)
the end the program and return to the form window.
Saving a Project
Choosing save project from the file menu. Visual Basic will prompt you
separately to save the form and then the project.
Example 2:
Design a form shown in figure below, with three text boxes and one
Command Button. Write code in the Command1 (Execute). So when run
project enter the Student Name in TextBox (Txt1) and the Father Name in
TextBox (Txt2). When click on Command1 (Execute) replace the Full
Name in the TextBox(Txt3).
Solution:
Example 3:
Design a form with Three Commands Button. Write code in the Command1
(Red),in the Command2(Green)and in the Command3(Blue).So when click on
Command1 (Red) theBackcolor with form change to red color , when click on
Command2(Green) the Backcolor with form change to Green color and when
click on Command3(Blue) the Backcolor with form change to Blue color.
Solution:
Private Subcmdblue_Click()
Form1.BackColor = vbBlue
End Sub
Private Subcmdgreen_Click()
Form1.BackColor = vbGreen
End Sub
Private Subcmdred_Click()
Form1.BackColor = vbRed
End Sub
Example4:
Design a form with one picture and two Commands Button. Write code in the
Command1 (Show)andin the Command2(Hide).So when click on Command1
(Show) the picture will show and active the Hide command andwhen click on
Command2(Hide) the picture will hide and active the show command .
Solution:
Picture1.Visible = True
Comshowe.Enabled = False
Comhide.Enabled = True
End Sub
Private SubComhide_Click()
Picture1.Visible = False
Comshowe.Enabled = True
Comhide.Enabled = False
End Sub
Example 5:
write code for move specific shape (circle). in left side .,right side , Down, Up
and Diagonal.
(Left+ , Top)
(0,0)
(Left , Top)
(Left+ , Top+)
(Left , Top+)
Data types control of internal storage of data in Visual Basic. There are a
number of variable data types that allow you to optimize your code for
speed and size.
3.2 Variables:
In Visual Basic, uses variable for storage values. must start with
character and maximum length 255 character and not contain any
point.
Declaration of a variable
The declaration means defining the variable type. The variable has to
be declared with the Dim Statement, supplying a name for the
variable:
4.Data type: it represent the type of Data that the variable will take ,
we can used any type of data
A variables name:
Must not exceed 255 characters. The names of controls, forms, and
modules must not exceed 40 characters.
Examples:
Dim X As Integer
Error examples:
Dim X As string : Dim A, B, C, X(Repeat the variable name at
the same time in two Dim statement)
Example :
Print x , y
String Function
Asc(x) Convert character to ASCII
Numerical Function
Abs(x) Absolute value of a number
Int(x) Integer of x
Examples:
C=Right(A,7) : → C=Name Is
E=Mid(A,3) → E=Name Is
Example 6:
Write a program to convert input string to uppercase
Solution:
Interface
Code
Private Sub Com1_Click()
Lab1.Caption = UCase(Text1.Text)
End Sub
Example 7:
Properties
Command1
Name: Command1
Caption:1st 3 chat
Command2
Name: Command2
Caption:last 4 char
Label1
Name: Label1
Caption:
Text1
Name: Text1
Text:
Code
Private Sub Command1_Click()
Label1.Caption = Left(Text1.Text,3)
End Sub
+ Addition
- Subtraction
* Multiplication
Arithmetic operator / Division
Mod Modulus
^ Exponentiation
((4+sin(30))/(a^6-b))^( ⁄ )
√
3.14/4*(a/100)^5.6
√ (-b+sqr(b^2-4*a*c))/(2*a)
Example8:
Solution:
Interface
Properties
Command1
Name: Command1
Caption:Compute
Label1
Caption:R
Label2
Caption:area
Text1
Name: Text1
Text:
Text2
Name: Text2
Text:
Code
Text2.Text = area
End Sub
Example 9:
x1 = 10 , y1=1
x2 = x1-y1 , y2=x1+y1
z = y2+(y1/x1) * (x2*y2)
solution
Dim x2 as integer
Dim y2 as integer
Dim z as single
Private Sub Com1_Click()
x1 = 10
y1=1
x2 = x1-y1
y2=x1+y1
z = y2+(y1/x1) * (x2*y2)
lab1.caption = z
End sub
Example 10:
6. Conditional statements
There are two types of conditional statements:
1- If statement
2- Select case
Sol:
Dim x as integer
Private sub command1_click()
X= val(text1.text)
If x>= 50 then text2.text= "pass"
End sub
If condition then
Statements
End if
Sol:
Dim x as integer
Private sub command1_click()
X= val(text1.text)
If x>= 50 then
text2.text= "pass"
form1.backcolor=vbgreen
end if
End sub
If condition then
Statements1
Else
Statements2
End if
Sol:
Dim x As Integer
Private Sub command1_click()
x = val(Text1.Text)
If x >= 50 Then
Text2.Text = "pass"
Else
Text2.Text = "fail"
End If
End Sub
If condition1then
Statements1
Elseifcondition2then
Statements2
.
.
.
Else
Statementsn
End if
Text3.Text = "a=b"
End If
End Sub
W.H:
Loop statement:
Visual basic supports statement to perform loops. The loops statements
could
Have different structures as follows:
1- Unconditional loop.
2- Conditional loop.
1- Unconditionalloop:
Loops apply programming statements for fixed number of times using
counter
(for… next) statement.
2- Conditional loop
Loops repeat programming statements according to specific condition.
There are
two types of conditional loop:
1- Do while
2- Do until
1-Do while loop: In this loop the statements will be implemented and
repeated whenever the condition satisfied.
The standard form is:
while condition
Statements
wend
Example3: Write a program to print (hello) five times with its numbering
using dowhile loop.
Sol:
Private Sub Command1_Click ()
Dim i as integer
i=1
whilei <= 5
Print "hello"; i
i=i+1
wend
End Sub
Code 1 Code 2
Private Sub comdisplay_click)( Private Sub comdisplay_click)(
i=1 Max = a
F=1 If b > Max Then
Do While (i <= 5) Max = b
F=F*i End If
i=i+1 If c > Max Then
Loop Max = c
Print F End If
End Sub End sub
W.H:
Q1:Write a program to enter the value of two variables (X and Y). Find and
print the maximum value for two variables. Design form window.
Q2:Design a form and write code to find the summation of even numbers (from 0
to 100)(use while).
Q3:Design a form and write code to find the summation of odd numbers (from 0
to 100).
Sol:
b
Example: enter value of a,b using inputbox and find a
Dim a As Integer
Dim b As Integer
Dim c As Integer
c=a^b
Label1.Caption = c
End Sub
2-Message box
It is used to output a message to the user (at running stage) the code
needed could be written in code sheet and in any event or command.
For example if we write the following statement then a message box will
be appear as shown below
X=MsgBox("do you want to close your program", 3, "close")
or
x=MsgBox(" do you want to close your program ",vbYesNoCancel, " close")
For example if we write the following statement then a message box will
be appear asshown below
X=MsgBox("please close your program", 16, "Error")
or
x=MsgBox "please close your program", vbcritical, "Error"
For example show what appear after running the following statement
X=MsgBox("are you sure you want to delete this file", 32 + 4, "delete")
or
x=MsgBox("are you sure you want to delete this
file",vbQuestion+vbYesNo,"delete")
z = Val(Text3.Text)
If z =(x + y) Then
a = MsgBox("the answer is correct", 1, "answer")
Else
b = MsgBox("the answer is error", 1, "answer")
End If
End Sub
List box:The user can't write directly in ListBox . He can add item to
the ListBoxproperty or by code in the form.
Listname.additem data
Sheet of Questions:
Q1: Write a VB code and properties to enter the value of two variables (X and
Y)by using inputbox. Find and print on form the maximum value for two
variables. Design form window?
Q2: Design a form and write VB code and properties to find the summation of
even numbers (from 0 to 100) (use while)?
-------------------------------------------------------------------------------------------------------
Q3: Design form and write VB code and properties to the following: you have (1
command, 1 list box) when click on command the negative numbers in listbox
will print on form?
-------------------------------------------------------------------------------------------------------
Q4: Read this code carefully, and write the AIM of this code: Write what the
output of the code if:
Q9: State the type of (for ..next) ,what will be (initial, final, step) for each type?
with examples.
--------------------------------------------------------------------------------------
Q10: Write VB code and properties to the following: you have (1command,
2listbox) when you click on command the first name and second name will print
on form?