Visual Basic 6
Visual Basic 6
Visual Basic 6
Course Information
Course objectives:
Understand basic theories on Computer software
programming
Understand Microsoft Visual Basic 6.0 syntax and its
major features
Design and develop GUI Windows applications
Develop critical thinking of software programming
Introduction
Computing concepts
Programming concepts
Flowchart
Computing Concepts
Computer
CPU
Processor
I/O
devices
I/O Devices
Storage system: Primary & Secondary
Storage
System
Operating system
User interface: command line , GUI
Programming concepts:
Whats Program
Programming languages: machine code, high level languages
Developing environmental
Algorithms
Programming languages
GO TO Programming : basic, assembly
Structured Programming: Pascal, C,..
Object Programming: Java, C++
Visual approach for GUI programming.
Compiler
Interpreter
Compiler
Whats Object?
Object properties
Object method
Whats Class?
Message/Even
Object vs. Structure
How can an OOP
program works?
Object and Visual
Basic
Methods
Object 1
Messages
Object 1
Object 1
Whats algorithms
How to present an algorithms?
Natural language, Programming languages
Others?
Flowchart
Characteristic: picture base, chart base
Capability
Advantage:
Simple
Easy to understand
Powerful
Better view and control of Flowchart
Flowchart components
Start or Begin of algorithms. One START point only.
Start
Process Flow
Yes
Evaluation Item
Stop
Value i
Others
Flowchart Example
Algorithms
ax + b=0 equation solving.
Start
Read a, b
a=0 ?
Yes
No
X=-b/a
b=0 ?
Yes
No
No Solution
Any number
is solution
Stop
10
11
12
13
IDE Overview
Menu Bar
Tool Bar
Project
Window
Properties
Window
Department of Information Technology
Nguyn Cao Tr [email protected]
15
Project View
Tool Box
Properties
Form
16
Tool Box
Properties Windows
Project Windows
Project folder view
Form view
Code view
17
Program description
The program will show/hide the text Welcome to
Visual Basic 6.0 when button Show/Hide is clicked
The exit button used to exit the program. It is available
after first click of Show button only.
Design form with Label object name Label1 and caption Welcome
to Visual Basic 6.0 Set it visible properties to false.
Add button cmdShowHide & Button cmdExit to form.
Click on each button for writing code to it base on Click even
Click Play button on IDE to run program.
18
Visible = false
Caption = welcome to Visual Basic
6.0
cmdExit
Enable = false
cmdShowHide
Caption =&Show
19
20
22
Introduction to
Visual Basic programming
Button (CmdButton)
Frame, Selected box, List box
24
ActiveX Control
Horizontal scrollbar
DriveListBox
Label
DirListBox
TextBox
FileListBox
CommandButton
Shape
Timer
Line
PictureBox
Data
Frame
OLE
CheckBox
Animation
OptionButton
UpDown
ComboBox
MonthView
ListBox
DateTimePicker
Vertical scrollbar
FlatScrollBar
25
Whats Even?
Component
evens
Even Procedure
26
27
Other Application
Accumulate ADD
28
Data type
Variable declaration
DIM variablename As datatype
Ex:
Dim Sum As Integer
Dim Total As Integer
Assign statement
Variable = Assigned value
29
Arithmetic
Visual Basic
Operation
Arithmetic
operator
Algebraic
expression
Visual basic
expression
Addition
Subtraction
Multiplication
Division (float)
Division (Integer)
Exponentiation
Negation
Modulus
+
*
/
\
^
Mod
x+y
xy
ab
v/u
None
Pq
-e
Q mod R
x+y
xy
a*b
v/u
v\u
p^q
-e
Q mod R
30
Operators Precedence
Operator
Operation
()
Parentheses
Exponentiation
Negation
* and /
Multiplication and
floating point
division
Integer Division
Mod
Modulus
+ or -
Addition and
Subtraction
Order of evaluation
Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If there
are several pairs at he same level nested, they are
evaluated left to right.
Evaluated second. If there are several, they are
evaluated left to right
Evaluation first. If there are several, they are evaluated
left to right.
Evaluation fourth. If there are several, they are
evaluated left to right.
Evaluation
left to right
Evaluation Firths. If there are several, they are evaluated
left to right
Evaluation Firths. If there are several, they are evaluated
left to right
31
Comparison Operators
Standard
Algebraic
equality
operator
(relational
operator)
Visual Basic
comparison
operator
Example of
Visual Basic
condition
>
<
=
<>
>
<
<=
d=g
S <> R
y>i
P <M
C <= E
>=
M >=s
D is equal to g
S is not equal to R
Y is greater than i
P is less than M
C is greater than or equal
to E
M is less than or equal to S
32
33
Visual Basic
Control structures
Understanding
control structures of VB
Flowchart and control structures
Using control structure by samples
If/Then structure
Syntax
if <condition> then
statements
end if
Example
Yes
Condition
Statements
35
If/Then/Else structure
Syntax
if <condition> then
statements
else
statements
end if
Condition
No
Example
Yes
Statements
Statements
36
While/Wend structure
Syntax
While <condition>
statements
Wend
Condition
Yes
Example
Statements
Whats on form
4 8 16 32 64 128 256 512 1024
No
37
Do while/Loop structure
Syntax
Do while <condition>
statements
Loop
While/Wend
Example
||
Do while/Loop
Whats on form
4 8 16 32 64 128 256 512 1024
38
Do until/Loop structure
Syntax
Do until <condition>
statements
Loop
Statements
Condition
Example
No
Whats on form
4 8 16 32 64 128 256 512 1024
Yes
39
For/Next structure
Syntax
Example
For j = 2 to 80 step 5
a= a+2
Next j
Counter = StartValue
Counter = Counter + Stepsize
statements
** Stepsize = 1 is default
value
No
40
Sample of For/Next
41
Syntax
42
Syntax
counter = 1
Do
Print counter & Space$(2);
counter = counter + 1
Loop While counter <= 10
Result
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
43
Syntax
counter = 1
Do
Print counter & Space$(2);
counter = counter + 1
Loop Until counter >= 10
Do
Statements
Loop While
Condition
Yes
No
Result
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Statements
Do
Loop Until
Condition
No
Yes
Department of Information Technology
Nguyn Cao Tr [email protected]
44
Result:
12345
Result:
12345
For x=1 to 10
if x=5 then
exit for
end if
print x
next x
45
Logical operators
NOT
AND
OR
46
Bytes
Range
Boolean
True or False
Byte
0 to 255
Currency
-922337203685477.5808 to 922337203685477.5807
Date
Double
Integer
-32767 to 32768
Long
-2147483648 to 2147483648
Object
Single
-3.402823E38 to -1.401298E-45
3.402823E38 to 1.401298E-45
String
10+
0 to ~2000000000 characters.
Variant
16
Any value within the ranges listed above. (See chapter 21)
47
LAB Works
48
Module programming
Whats module?
Why do we need to write program in module?
Modules in Visual Basic
Even procedure
Visual basic procedure
SUB procedure
Function procedure
50
SUB procedure
Public Sub
MyProcedure()
Your process codes
here
End Sub
Department of Information Technology
Nguyn Cao Tr [email protected]
51
52
Function Procedure
53
54
55
56
Overdrive declaration
57
What s recursion?
Recursion part
Stop recursion condition
Recursion :
Iteration :
n! = n * (n-1)!
0! =1
n! = 1*2*3*..*n
Benefit of Recursion
The weakness of recursion in programming
Hanoi tower solution
58
Optional arguments
Private Sub FooBar ( y as Boolean, Optional z as long)
Calling
Call FooBar
Call FooBar (True)
Call FooBar (True ,10)
59
Named Arguments
Public Sub Display (flag As Boolean, number As Long, name As String)
Print
Print flag
Print number
Print name
Print
End Sub
60
Code modules
61
Review
62
Array structure
Introduce Array structure
Understand using array structure; sort, search,..
Multi dimensions
Dynamic array
87 16 34
Default
Example :
Lbound =0
Ubound = Number of Items -1
Dim myarray(9) As Integer
Dim Mysecondarray (1 to 10) As Integer
Arrayname(index)
64
65
66
67
68
69
70
Sorting array
71
72
73
Linear Search
74
Function LinearSearch(a() As
Integer, key As Integer) As Integer
Dim x As Integer
For x = LBound(a) To UBound(a)
If a(x) = key Then
LinearSearch = x ' Return index
Exit Function
End If
Next x
LinearSearch = -1
found
End Function
75
Binary Search
76
77
78
Multidimensional array
Multidimensional array
Col 1
Col 2
Col 2
Row 1
Item(1,1)
Item(1,2)
Item(1,3)
Row 2
Item(2,1)
Item(2,2)
Item(2,3)
Row 3
Item(3,1)
Item(3,2)
Item(3,3)
arrayB(3,2,7,0,0)
79
80
Control Array
Select a control -> copy, past it to the same form as many control as you
need, they will be a control array
Using the same name for controls
81
82
Dynamic array
83
84
85
86
Character
88
89
90
91
92
S1=Hello
S2 = friends
S 3 = S1 + S2 => S3 = Hello friends
S4 = S1 & S2 => S4 = Hello friends
93
Operator Like
94
95
where the substring exist from startpos of string or return 0 if not exist.
Function InsStrRev (String , SubString , Endpos) Return the
position where the substring exist from endpos of string or return 0 if not
exist.
Department of Information Technology
Nguyn Cao Tr [email protected]
96
97
Conversion Functions
98
99
100
Graphic
Coordination system in computer
graphic
Drawing on window with VB
Coordinate system
User define
coordinate system
(0,0)
x
(0,0) x
(x,y)
(x,y)
102
Constant
Value
Description
User defines
vbUser
Tswips
vbTwips
Points
vbPoints
Pixels
vbPixels
Characters
vbCharacters
Inches
vbinches
Physical inch.
Millimeters
vbMillimeter
Physical Millimeters.
Centimeters
vbCentimeter
Physical centimeter.
103
104
Drawing Methods
105
Draw Method
106
107
108
109
110
Drawing Properties
Draw properties used to define how do items be drawn on form.
Property
Description
AutoRedraw
DrawMode
DrawStyle
Drawwidth
FillStyle
111
Value
Description
vbBlackness
vbNotMergePen
vbMaskNotpen
vbNotCopyPen
vbMaskNotPen
Mask Not Pen combines the color common to both the pen and the
inverse of the display.
vbInvert
vbXorPen
Xor Pen combines the colors in the pen and in the display color ,
but not in both.
vbNotMaskPen
vnMaskPen
Mask Pen combine the colors common to both the pen and the
display.
vnNotXorPen
10
112
Value
Description
vbNop
11
vbMergeNotPen
12
Merge Not Pen combines the display color and the inverse of the
pen color.
vbCopyPen
13
vbMergePenNot
14
Merge Pen Not Combines the pen color and the inverse of the
display color.
vbMerPen
15
Merge Pen Combines the pen color and the display color.
vbWhiteness
16
113
DrawMode Sample
114
Value
Description
vbSolid
vbDash
vbDot
vbDashDot
vbDashDotDot
vbInvisible
vbInsideSoloid
115
DrawStyle Sample
116
Value
vbFSSolid
vbFSTransparent
vbFSHorizontalLine
vbVerticalLine
vbUpwardDiagonal
vbDownwardDiagonal
vbCross
vbDiagonalCross
Description
117
FillStyle sample
118
119
120
121
Colors
122
Colors Sample
ForeColor = RGB(Rnd() * 256, Rnd() *
256, Rnd() * 256)
a = 3 * Rnd() ' Offset used in equation
For theta = 0.001 To totalRadians Step
0.01
r = Sqr(a ^ 2 / theta)
x = r * Cos(theta) ' y coordinate
y = r * Sin(theta) ' x coordinate
x1 = -r * Cos(theta) ' y coordinate
y1 = -r * Sin(theta) ' x coordinate
PSet (x, y)
' Turn pixel on
PSet (x1, y1)
' Turn pixel on
Next theta
End Sub
Department of Information Technology
Nguyn Cao Tr [email protected]
123
124
PictureBox Sample
Private Sub Form_Load()
picPicture.Picture = LoadPicture("d:\images\ch09\cool.bmp")
End Sub
Private Sub cmdInvert_Click()
Call picPicture.PaintPicture(picPicture.Picture, 0, 0, , , , , , , vbDstInvert)
Call SavePicture(picPicture, "images\" & "cool_inverse.bmp")
End Sub
125
Printer Object
Printer Object allow user sent text / graphic to printer. By default printer
object s properties correspond to default printer in windows setup.
126
Printer object
127
Controls
User graphic
interface in VB is
designed by using
controls
Standard Control:
building with VB
ActiveX control
Developed by others
Need to add to VB by
select Projects>Components menu
Menu design
129
Standard Controls
130
Standard Controls
131
Standard Controls
132
TextBox
133
TextBox
134
135
136
Telephone
6290
(091) 391-
137
Menu design
138
139
140
141
Arranged Constant
142
144
145
146
For x = 1 To 100
Put #1, x, udtBlankClient ' Write empty
records
Next
Close #1 ' Close file
cmdInitialize.Enabled = False
MsgBox ("File initialized. Click Exit to
terminate.")
Else
MsgBox ("You must specify a file name")
End If
End Sub
147
File Open
dlgOpen.ShowOpen
148
Common Dialog
149
dlgOpen.ShowOpen
filename = dlgOpen.filename
If dlgOpen.FileTitle <> "" Then
' Open file for writing
Open filename For Random Access Read As #1 Len = recordLength
cmdOpenFile.Enabled = False ' Disable button
150
Database applications
Database management system (DBMS)
Structured query Language (SQL)
Using database with VB
Relationship database
Database Management System (DBMS)
Visual basic, relationship database and SQL
152
153
154
155
156
157
158
SQL Command
159
SQL Command
160