Programming With Visual Basic
Programming With Visual Basic
.
8-General view on assignment statement, constants and operations.
.
11-If-hen-els statement.
12-Select statement.
13-For-next statement.
14-Condition loop .
15-Nested-loop.
16-Seguences.
Microsoft Visual
Basic
6.0
Visual Basic
.
Basic
Beginning All purpose Symbolic Instruction Code "
" ) (VB ) (Windows
Dos
... .
VB graphical
user interface
.
VB
Objects Properties
Actions Events
:
Start>Programs>Microsoft Visual Studio 6.0>Microsoft Visual Basic 6.0
. ) (
VB .
Visual Basic
a
b
f
d
e
(a :Menu bar
.
(b :Toolbar .
(c :Toolbox Controls
.. :
( )(Pointer
: .
( )(Picture box
( )(Label
: .
( )(Text Box
( )(Frame
: .
.: .
( )(Command Button
( )(Check Box
: .
:
( )(Option Button
.
/
( )(Combo Box
Visual Basic
: Drop-
.Down list
: .
( )(List Box
( )(Horizontal Scrollbar
( )(Vertical Scrollbar
( )(Timer
: .
: .
: .
:
:
:
.
( )(Shape
( )(Line
( )(Image
:
: .
).(Stretch
( )(Data
( )(OLE
: .
: .
(d :Form Window
.
:Properties Window object
.
(g :Code Window
. Code Private sub
Visual Basic
End sub .
.
) ( VB
.
:
( ) :(design objects
toolbox
Visual Basic
) (
)Project1-Microsoft Visual Basic (design
( ) :(Code object
)Project1-Form1 (Code
) :(Run Form
Start
Run
. End
Run
.
:
. VB
. )( Label
: :
Visual Basic
""=Labelno.caption
caption
)(Labelno.backcolor =QBcolor
backcolor
)(Labelno.forecolor =QBcolor
forecolor
)(= Labelno.fontsize
= or Labelno.fontsize
Fontsize
: " " .
:
: label
label .
caption " "Label1 " ".
Font size
: .
Visual Basic
) (
""=textno.text
Text
)(textno.backcolor =QBcolor
backcolor
)(textno.forecolor =QBcolor
forecolor
Multiline True
Multiline
Multiline False
Enabled
textno.Enabled=true
textno.Enabled=false
text
text
delete .
:
.
Te
:
. Command Button
:
Visual Basic
""=commandno.caption
caption
1-Graphical
0-Standard
Style
)(commandno.backcolor =QBcolor
backcolor
style
1-Graphical
Comma
command1 .name
command1 :
)(
/ form .
/ Calculator ) (
.
/ :
Visual Basic
. Form
VB form
: object
.form
name
)(0-15
)(0-15
" "=formno.caption
caption
) (
)(formno.backcolor=QBcolor
backcolor
)(formno.forecolor=QBcolor
forecolor
. Shape
:
0-rectangle
1-square
2-oval
3-circle
0-trancsparence
1-opaque
shape
backstyle
backcolor
bordercolor
: .
) : (
shape1 shape
) ( shape shape1
) 3- circle (.
Visual Basic
) label1 ( caption
label1 .circle
Sha
Lab
. . List box
- :design stage
list Listno )
( .
display .
:
: .form2
Form1
" "=Label1: caption
"Command1: caption="show
Visual Basic
"Command2: caption="hide
Label1
Command2
Command1
form2 :
. Option Button
:
Visual Basic
qbcolor vb
.
Private Sub Option1_Click()
Form1.BackColor = vbGreen
End Sub
Private Sub Option2_Click()
Form1.BackColor = vbBlue
End Sub
Private Sub Option3_Click()
Form1.BackColor = vbRed
End Sub
Check Box
Caption, Font, Enabled, BackColor, Forecolor, value and :
visible
.Text Box :
: text . : :
Text1
Text
Text1
Check1
caption Bold
Check2
Check1
caption Italic
Check2
Check3
caption underline
Check3
Private Sub Check1_Click()
Text1.FontBold = Check1.Value
End Sub
Private Sub Check2_Click()
Text1.FontItalic = Check2.Value
End Sub
Private Sub Check3_Click()
Text1.FontUnderline = Check3.Value
End Sub
Visual Basic
. Timer
:
Interval
) (
)(true/false
enabled
timer interval
: .
: : timer interval timer1
1000 label caption
label1 .
Label
Timer
:
)(Private Sub Timer1_Timer
Label1.Caption = Time
End Sub
: time . label1
. Input-Output Boxes
Input Box .Message Box
.
Message box
.Msgbox
Msgbox:
"Msgbox "Message", Symbol, "title
Visual Basic
vbcritical
16
32
vbquestion
vbexclamation
48
vbinformation
64
: )( Commands
Vbokonly
Vbokcancel
vbAbortRetryIgnor
vbYesNoCancel
vbYesNo
vbRetryCancel
0
1
2
3
4
5
Commands
Ok
Ok, Cancel
Abort, Retry, Ignore
Yes, No, Cancel
Yes, No
Retry, Cancel
:
Visual Basic
Input Box
Message Box
ok . :
)"X = Inputbox ("prompt","title
:
:Prompt .
:Title ) (caption .
X .
: x .inputbox
: form
.
)(Private Sub Form_Load
)"X=Inputbox("enter value of x", "calculation
End Sub
calculation enter value of x
x .
Visual Basic
.
.
.
. ):(Variables
As Dim
:
:Integer .
:Long ) (.
:Single ) (.
:Double ) (.
:String .
:Boolean ).(true or false
:Date
:Time .
. Constant
:
= Const
=. : :
Const = .
.
:
.
.
.
.
Visual Basic
):(assignment statement
:
Variable name = expression
:
.
)(Operation Code
^
) (Operation
*/
Mod
+-
) (
&
>< = < <= > >=
AND , OR
) (
)3*(x+4*y
)3(x+4y
X^2 + 4 / 2
X2 + 4 2
16 + 33 + 10 5 4 3 2 2 3
(5*y)/(x^2-4)+x-1
5y
+ x 1
x 4
2
:
:
) ( : Val .
) ( :Cstr .
) ( :CInt .
Visual Basic
.
( :Input
:
textbox .
inputbox .
( :output :
textbox .
label .
message box .
. print
: form )(
.
: :
form
caption
Command1
caption
+
Command2
Caption
Label1
Caption
Label2
Caption
text1, text2 text
Dim x, y, z as integer
)( Private sub command1_click
)X=val(text1.text
)Y=val(text2.text
Z=x + y
)="&z "(Msgbox
End sub
)( Private sub command2_click
)X=val(text1.text
)Y=val(text2.text
Z=x - y
)="&z "(Msgbox
End sub
Visual Basic
.)(string
,"2007" ,"100$" ,"name" :
" " )" "(
:
As String Dim
: ) ( Len Length
.
: .
.:
Form1
Caption
String length
Command1
caption
Click here
Label1
Caption
=Length
Text1
Text
.
Dim s As String
)(Private Sub Command1_Click
)"s = InputBox("inter string
)L = Len(s
)Text1.Text = CStr(L
End Sub
End sub
.
)abs(x
)sqr(x
) sin,cos,tan x(
Visual Basic
:
.print
.:
text1, text2, text3 command1
caption .run print
.
.:
Dim a, b, c, av As single
)( Private sub command1_click
)a=val (text1.text
)b=val(text2.text
)c =val(text3.text
av=(a+b+c) / 3
Print "av=";av
End sub
)(
/ .
/ input box
.label1, label2
/ .
/ : run :
Visual Basic
. ):(conditional statements
- )(if statement
- )(select case
- ):(if statement
if <, >, <=, >=, < >, =, and, or
if.
( If ..Then
:
If condition then statement
: pass
:
:
:
Dim x as integer
)(Private sub command1_click
)X= cint(text1.text
"If x>= 50 then text2.text= "pass
End sub
( If block
:
If condition then
Statements
End if
Visual Basic
pass :
:
Dim x as integer
Private sub command1_click()
X= cint(text1.text)
If x>= 50 then
text2.text= "pass"
text2.fontsize=18
end if
End sub
If.. Then.. Else (
:
If condition then
Statements
Else
Statements
End if
:
.
:
Dim x As Integer
Private Sub command1_click()
x = CInt(Text1.Text)
If x >= 50 Then
Text2.Text = "pass"
Else
Text2.Text = "fail"
End If
End Sub
If.. then.. Elseif ..Else (
:
If condition then
Statements
Elseif condition then
Visual Basic
Statements
Else
Statements
End if
:
.
:
Dim x As String
Private Sub command1_click()
x = text1.Text
If x = "Muhammad" Then
MsgBox "hello, Muhammad"
ElseIf x = "hasan" Then
MsgBox "hello,hasan"
Else
MsgBox "hello, guest"
End If
End Sub
Visual Basic
If b <> 0 Then
Text3.Text = CStr(c)
Else
Text3.Text = "illegal division operation"
End If
End If
End Sub
Select Statement -
:
Visual Basic
Case 4
)"MsgBox ("Thursday
Case 5
)"MsgBox ("Wednesday
Case 6
)"MsgBox ("Friday
Case 7
)"MsgBox ("Saturday
End Select
End Sub
. loop statement
visual basic
- : -
- counter loop
:fornext
:
Visual Basic
:
Do while condition
Statements
Loop
: hello do while
Dim i as integer
)( Private Sub Command1_Click
i=1
Do while i <= 5
Print "hello"; i
i=i+1
Loop
End Sub
:Do until Loop - Do until
:
:
: hello
Do until condition
Statements
Loop
Dim i as integer
)( Private Sub Command1_Click
i=1
Do
Print "hello"; i
i=i+1
Loop until i > 5
End Sub
:
Exit:
:For next - .Exit for
Visual Basic
.
.
: x s=1+x+x2+x3++xn
:
Dim I, n, sum as integer
Dim x,s as Double
)( Private Sub command1_click
sum = 1
)n=cint(text1.text
)x=val(text2.text
For i = 1 To n
Sum = sum +x^i
Next i
)Text3.text=cstr(sum
End Sub
:nested for
.
For i = 1 To n
For j = 1 To m
Statements
Next j
Next i
:
Dim I, j As Integer
)(Private Sub command1_click
For I = 1 To 10
For j = 1 To 10
p=I*j
Print I; "*"; j; "="; p,
Next j
Print
Next I
End Sub
Visual Basic
/ x y
Y=x x
y=-x x
/ label
/ check box
option button .
/
) (
/
9-10
A
7-8
B
5-6
C
0-4
D