Visual Basic 6
Visual Basic 6
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
Text book/reading material:
Visual Basic 6.0 how to program ,Deitel & Deitel
Cyber classroom CD :Visual Basic 6.0 how to program
Internet Web site access
Computing concepts
Programming concepts
Flowchart
Computer CPU
Processor I/O Storage
I/O Devices devices System
Programming languages
GO TO Programming : basic, assembly
Structured Programming: Pascal, C,..
Object Programming: Java, C++
Visual approach for GUI programming.
Compiler
Interpreter
Compiler
Integrated Development Environment (IDE)
Project manager, Code editor, debug, compiler
What’s 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
• Process Flow
No
Condition • Process Direction Control
Yes
Evaluation Item
• Other Process direction Control
Value 1 Value i Others
Yes Yes
a=0 ? b=0 ?
No No
Stop
Tool Bar
Project
Window
Properties
Window
Project View
Tool Box
Properties
Form
Tool Box
Content all objects/components for FORM design.
Properties Windows
Control all properties of Form/Objects/Components
Display, font, format, enable,…
Project Windows
Project folder view
Form view
Code view
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.
How to do that with Visual Basic 6.0
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.
See sample below
form1
Label1
Visible = false
Caption = “welcome to Visual Basic
6.0”
cmdExit
Enable = false
cmdShowHide
Caption =“&Show
First run: Only Show button available Click Show button: Exit enable, show Text,
Show -> Hide
Click Hide button: Hide Text Click Exit button: Exit Program
What’s Even?
Component
evens
Even Procedure
The sub procedure will
be done when an
even happened to
component
Each component has its
own evens.
•CODE
Private Sub Command1_Click()
Print " Hello! Welcome to Visual
Basic"
End Sub
Data type
Variable declaration
DIM variablename As datatype
Ex: Dim Sum As Integer
Dim Total As Integer
Keywords and reserved words
Statement
Statement end with “end of line”
Assign statement
Variable = Assigned value
Department of Information Technology
Nguyễn Cao Trí – [email protected] 29
Arithmetic
Visual Basic Arithmetic Algebraic Visual basic
Operation operator expression expression
Syntax Example
if <condition> then If cmdShowHide.Caption = “&Show” then
cmdShowHide.Caption = “&Hide”
statements
Label1.Visible = true
end if
end if
Yes
Condition
Statements
Syntax Example
if <condition> then If cmdShowHide.Caption = “&Show” then
cmdShowHide.Caption = “&Hide”
statements
Label1.Visible = true
else
else
statements cmdShowHide.Caption = “&Show”
end if Label1.Visible = false
end if
No
Condition
Yes
Statements Statements
Syntax Example
While <condition> Dim product As integer
product = 1
statements
Wend
While product <= 1000
product = product * 2
print product
Wend
Yes
Condition Statements
What’s on form
No 4 8 16 32 64 128 256 512 1024
Syntax Example
Do while <condition> Dim product As integer
statements product = 1
Loop
Do while product <= 1000
product = product * 2
print product
While/Wend Loop
||
What’s on form
Do while/Loop 4 8 16 32 64 128 256 512 1024
Syntax Example
Do until <condition> Dim product As integer
product = 1
statements
Loop
Do until product >1000
product = product * 2
print product
Statements Loop
Condition
No
Syntax
For counter=startValue To endValue Step Stepsize
statements
Next counter
Example
For j = 2 to 80 step 5
Counter = StartValue
a= a+2
Next j Counter = Counter + Stepsize
statements
Syntax
Select Case mAccessCode
Case Is < 1000
message = "Access Denied"
Beep
Case 1645 To 1689
message = "Technician
Personnel"
Case 8345
message = "Custodial Services"
Case 55875
message = "Special Services"
Case 999898, 1000006 To
1000008
message = "Scientific Personnel"
Case Else
message = "Access Denied"
End Select
Department of Information Technology
Nguyễn Cao Trí – [email protected] 42
Do/loop while structure
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
Syntax
counter = 1
Statements Do
Do
Print counter & Space$(2); Loop While
counter = counter + 1 Yes
Loop Until counter >= 10 Condition
No
Result
Statements
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 Do
1 2 3 4 5 6 7 8 9 10 No
Loop Until Condition
Yes
Result: Result:
12345 12345
Logical operators
NOT
AND
OR
Where ‘s Logical operators used
Procedure in VB
Sub/Function
Recursive
What’s module?
Why do we need to write program in module?
Modules in Visual Basic
Even procedure
Visual basic procedure
SUB procedure
Function procedure
What should be used ?
Visual basic procedures
Sub/function procedures
Public Sub
MyProcedure()
Your process code’s
here
End Sub
Department of Information Technology
Nguyễn Cao Trí – [email protected] 51
SUB procedure sample
Public Sub MyProcedure2(textstr As String)
Print textstr
End Sub
What ‘s recursion?
Recursion part
Stop recursion condition
Sample of recursion: n factorial defined
Recursion : n! = n * (n-1)!
0! =1
Iteration : n! = 1*2*3*……..*n
Benefit of Recursion
The weakness of recursion in programming
Hanoi tower solution
Department of Information Technology
Nguyễn Cao Trí – [email protected] 58
Optional arguments
12 24 32 1 87 16 34 5
Multidimensional array
Col 1 Col 2 Col 2
Row 1 Item(1,1) Item(1,2) Item(1,3)
Call lstValues.Clear
arrayLength = txtInput.Text
Result
-1 FirstStr < SecondStr
1 FirstStr > SecondStr
0 FirstStr = SecondStr
Option Type
vbBinaryCompare
vbTextCompare
vbDatabaseCompare
vbUseCompareOption
Option Compare Type
Binary Text Database
(0,0) x
(0,0) x
y
(x,y)
y
(x,y)
End Sub
Property Description
AutoRedraw A Boolean value that determones whether or not copy all
graphic element drawn is store in memory
DrawMode An Integer value that specifies how new points generated
from Line, Circle and Pset are draw (see DrawMode slide)
DrawStyle An Integer value that specifies how new points generated
from Line, Circle and Pset are draw (see DrawStyle slide)
Drawwidth An Integer value in range 1 to 32767 that specifies the
drawing width in pixels.
FillStyle An Integer value that specifies how new points generated
from Line, Circle and Pset are drawn.(see FillStyle slide)
Private Sub
cmdButton_Click()
Dim v As Integer
v = txtStyle.Text
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
dlgOpen.ShowOpen
File open dialog
cmdOpenFile.Enabled = False
cmdEnter.Enabled = True
cmdDone.Enabled = True
Else
MsgBox ("You must specify
a file name")
End If