Visual Basic 6.0
-
Upload
palitha-baddegama -
Category
Education
-
view
3.168 -
download
2
description
Transcript of Visual Basic 6.0
Palitha Baddegama.Divisional Computer Resource Centre
Hingurakgoda
Visual basic 6.0Visual basic 6.0
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Form Window
Title barMenu Bar
Tool BarT
oo
l B
ox
Code Window
Project Window
Property Window
Layout Window
(IDE)Integrated Development
Environment
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
New Project Dialog Box
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Control Description
Pointer Provides a way to move and resize the controls form
PictureBox Displays icons/bitmaps and metafiles. It displays text or acts as a visual container for other controls.
TextBox Used to display message and enter text.
Frame Serves as a visual and functional container for controls
CommandButton Used to carry out the specified action when the user chooses it.
CheckBox Displays a True/False or Yes/No option.
OptionButton Option Button control which is a part of an option group allows the user to select only one option even it displays multiple choices.
ListBox Displays a list of items from which a user can select one.
ComboBox Contains a Textbox and a List Box. This allows the user to select an item from the dropdown List Box, or to type in a selection in the Textbox.
HScrollBar and VScrollBar
These controls allow the user to select a value within the specified range of values
Timer Executes the timer events at specified intervals of time
DriveListBox Displays the valid disk drives and allows the user to selectone of them.
DirListBox Allows the user to select the directories and paths, which are displayed.
FileListBox Displays a set of files from which a user can select thedesired one.
Shape Used to add shape (rectangle, square or circle) to a Form
Line Used to draw straight line to the Form
Image used to display images such as icons, bitmaps and metafiles.
But less capability than the Picture Box
Data Enables the use to connect to an existing database anddisplay information from it.
OLE Used to link or embed an object, display and manipulate data from other windows based applications.
Label Displays a text that the user cannot modify or interact with.
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
View CodeView ObjectToggle FoldersProject NameForms FolderForm & modules
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
8
Name PropertyName Property
Must begin with a letterCan contain letters, numbers, and the
underscore character onlyMust not contain punctuation characters or
spaces (!,~,`,@,#,$,%,^,&,*,(,),-,=,+,|,\,/,>,<, ,,; ,:)
Must not exceed 40 characters
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Hungarian NotationHungarian NotationUse the three-character IDs shown in
Figure 1-10
Object First three Characters Ex:
Form frm frmAddtion
Command Button cmd cmdStart
Label lbl lblEnd
Text Box txt txtFirst
Menu mnu mnuExit
Check box chk chkChoice
Combo box cmb cmbFont
Figure 1-10
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
StatementsProcedures
◦Event Procedures◦Sub Procedures◦General Procedures◦Functional Procedures
Comments◦Using single quotation (‘)◦REM
Visual Basic's 6 Most Common Programming Statements
Statement type Examples
Declarations - define the name, type and attributes of all program variables
Dim Num as Integer ' declares a variable "Num" to be an IntegerDim vals(5) as Double ' declares an array of 5 Doubles named "vals"
Assignment - set values for the variables
Num = Num/10 ' after the assignment Num is set to 1/10th of its former valueHiString = "Hello " + "World" ' value of HiString is set to "Hello World"
Conditionals - do operations depending on the value of one or more variables
if Num > 0 then Num = Num * 2 ' Basic logic building blockSelect Case .... End Case 'Case block simplifies GUI programming
Iterations - control looping for repeated operations
for i = 1 to 5 'For-loop counts through a precise number of stepswhile ( val(i) > val(imin) ) 'While loops as long as condition remains True
Subroutines - calls on functions and subroutines
Private Sub Form_Load() 'A subroutine does not return a valuePrivate Function Digit() ' A function returns a value
Special statements - used to implement unique features
Set MyObject = Your Object 'Set statement assigns object referencesPrint #FileNum, MyObject.Text 'I/O statements like Print, Input Line
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Operator Mathematical function Example
^ Exponential 2^4=16
* Multiplication 4*3=12, (5*6))2=60
/ Division 12/4=3
ModModulus(return the remainder from an
integer division)
15 Mod 4=3 255
mod 10=5
\Integer Division(discards the decimal
places)19\4=4
+ or & String concatenation"Visual"&"Basic"="Visu
al Basic"
Arithmetic Operators
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Operator Meaning
= Equal to
> More than
< Less Than
>= More than and equal
<= Less than and equal
<> Not Equal to
Conditional Operators
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Operator Meaning
And Both sides must be true
or One side or other must be true
Xor One side or other must be true but not both
Not Negates truth
Logical Operators
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Variable Names
The following are the rules when naming the variables in Visual Basic
• It must be less than 255 characters • No spacing is allowed • It must not begin with a number • Period is not permitted • Reserved Word not allowed Ex. Sub, Private, End
Variable and Data Types
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Data TypesData Types
NumericStringDateBooleanObjectVariant
o Integero Floating
pointo Currencyo Byte
Integer Long Single Double
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Type Storage Range of ValuesByte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
Single 4 bytes -3.402823E+38 to -1.401298E-45 for negative values 1.401298E-45 to 3.402823E+38 for positive values.
Double 8 bytes
-1.79769313486232e+308 to -4.94065645841247E-324 for negative values 4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use +/- 7.9228162514264337593543950335 (28 decimal places).
Numeric Data Types
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Data Type Storage Range
String(fixed length) Length of string 1 to 65,400 characters
String(variable length) Length + 10 bytes 0 to 2 billion characters
Date 8 bytesJanuary 1, 100 to December 31, 9999
Boolean 2 bytes True or FalseObject 4 bytes Any embedded object
Variant(numeric) 16 bytesAny value as large as Double
Variant(text) Length+22 bytesSame as variable-length string
Nonnumeric Data Types
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
There are three levels of scope: project-level (also called "global" or "application" scope; the variable is accessible to all procedures in all modules of the project)module-level (the variable is accessible to all procedures in the module in which it is declared)local-level (the variable is accessible only to the procedure in which it is declared)
Variable ScopeVariable Scope
Pal
itha
Bad
dega
ma
, Com
pute
r R
esou
rce
Cen
tre,
Hin
gura
kgod
a
Variable ScopeVariable Scope
Form1 Form2 Dim Y as Integer Dim Z as Integer
Sub procedure 1 ()Dim A as Double....End Sub
Sub procedure 2 ()Static B as Double....End Sub
Sub procedure 3 ()Dim C as Double....End Sub
Module1Public X as Integer
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
MsgBox ( ) FunctionA=MsgBox(Prompt, Style Value, Title)
Example:
A=MsgBox( "Click OK to Proceed", 1, "Startup Menu") A=Msgbox("Click OK to Proceed". vbOkCancel,"Startup Menu")
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Button Layout Value Short Description
vbOKonly 0 Displays the OK button.
vbOKCancel 1 Displays the ok and cancel button.
vbAbortRetryIgnore 2 Displays the Abort , Retry , Ignore
vbYesNoCancel 3 Displays Yes , No and Cancel button
vbYesNo 4 Displays the Yes / No button
vbRetryCancel 5 Displays the retry and Cancel buttons
Style Values
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Value Named Constant Button Clicked
1 vbOk Ok button
2 vbCancel Cancel button
3 vbAbort Abort button
4 vbRetry Retry button
5 vbIgnore Ignore button
6 vbYes Yes button
7 vbNo No button
Return Values and Command Buttons
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
testmsg = MsgBox("Click to test", 1, "Test message")
testMsg2 = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message")
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Value Named Constant Icon
16 vbCritical
32 vbQuestion
48 vbExclamation
64 vbInformation
The Icons displayed in the message box are here
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
InputBox( ) Function A=InputBox(Prompt, Title, default_text, x-position, y-position)
B= InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
The numeric functions
a) Int Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on.
b) Sqr Sqr(4)=2, Sqr(9)=3 and etc.c) Abs Abs(-8) = 8 and Abs(8)= 8.d) Exp Exp(1)=e1 = 2.7182818284590e) Fix and Int Fix(-6.34)= -6 while Int(-6.34)=-7 f) Round Round (7.2567, 2) =7.26g) Log Log 10= 2.302585
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Len (“welcome to VB tutorial”) = 22Right ("Visual Basic", 4) = asicLeft ("Visual Basic", 4) = VisuLtrim (" Visual Basic")= Visual basicRtrim ("Visual Basic ") = Visual basicTrim (" Visual Basic ") = Visual basicMid ("Visual Basic", 3, 6) = sual BInstr (1, "Visual Basic"," Basic")=8Chr (65)=A, Chr (122)=z, Chr (37)=% ,Asc (“B")=66, Asc(“&")=38
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Format Syntax Result
Format(Now,"m/d/yy") 10/02/09
Format(Now,"dddd,mmmm,dd,yyyy") Friday,October 02, 2009
Format(Now,"d-mmm") 02-Oct
Format(Now,"mmmm-yyyy") October -2009
Format(Now,"hh:mm AM/PM") 09:36 AM
Format(Now,"h:mm:ss a/p") 09:40 a
Format(Now,"d-mmmm h:mm") 20-October 09:41
Date format
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Format Syntax Result
Format(Number,"# # # #.# #") 456.68
Format(Number,".# #") 456.68
Format(Number,"0000.00") 0456.68
Format(Number,"000000.00000") 000456.67680
Format(Number,"# # # # #.# # # # #") 456.6768
Format(Number,"# # # # #.0000") 456.6768
Number formatNumber = 456.6768
Format (n, "style argument")Format (n, "style argument")
Format (8972.234, "General Number")Format (8972.2, "Fixed")Format (6648972.265, "Standard")Format (6648972.265, "Currency")Format (0.56324, "Percent")
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Control StructuresControl Structures
Branching◦IF
IF Then IF Then – End IF IF Then Else – End IF IF Then Else IF Nested IF
◦Select Case◦Goto
Looping◦While -Wend◦Do while-Loop
◦Do-Loop while◦Do Until-Loop
◦Do-Loop Until◦For next
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
IF ThenIF Then
Condition Statement1True
False
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
IF Then – End IFIF Then – End IF
Condition
Statement 1Statement 2Statement 3Statement 4……………..Statement n
True
False
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
IF Then Else – End IFIF Then Else – End IF
ConditionTrue
False
Statement 1Statement 2Statement 3Statement 4……………..Statement n
Statement 1Statement 2Statement 3Statement 4……………..Statement n
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Statement 1Statement 2Statement 3……………..Statement n
A
Statement 1Statement 2Statement 3……………..Statement n
B
Statement 1Statement 2Statement 3……………..Statement n
C
IF <Condition> Then
Else
End IF
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
IF Then Else IFIF Then Else IF
ConditionTrue
False
Statement 1Statement 2Statement 3……………..Statement n
Statement 1Statement 2Statement 3……………..Statement n
Condition
False
Statement 1Statement 2Statement 3……………..Statement n
Statement 1Statement 2Statement 3……………..Statement n
True
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Statement 1Statement 2……………..Statement n
A
Statement 1Statement 2……………..Statement n
B
Statement 1Statement 2……………..Statement n
C
IF <Condition> Then
Else
End IF
Statement 1Statement 2……………..Statement n
D
ElseIF <Condition> Then
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Nested LoopNested Loop
ConditionTrue
False
Statement 1Statement 2Statement 3……………..Statement n
Statement 1Statement 2Statement 3……………..Statement n
Condition
False
Statement 1Statement 2Statement 3……………..Statement n
Statement 1Statement 2Statement 3……………..Statement n
True
Palitha B
addegama , C
omputer
Resource C
entre, Hingurakgoda
Statement 1Statement 2……………..Statement n
A
IF <Condition 2> Then Statement 1Statement 2……………..Statement m
ElseStatement 1Statement 2……………..Statement p
B
C
IF <Condition 1> Then
End IF
Statement 1Statement 2……………..Statement q
D
Else
Select CaseSelect Case
Select case TestExpressionCase Value1
Statement1Case Value2
Statement2Case Value3
Statement3…………………
Case ElseElse Statements
End Select
GoTo StatementGoTo Statement
Label :
Goto LabelorGoto Label
Label :
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
While -WendWhile -Wend
Condition
Statement 1Statement 2Statement 3
………………Statement n
True
FalseWhile Condition
Statement 1Statement 2……………..Statement
mWend
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Do While - LoopDo While - Loop
Condition
Statement 1Statement 2Statement 3
………………Statement n
True
FalseDo While Condition
Statement 1Statement 2……………..Statement
mLoop
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Do – Loop WhileDo – Loop While
Condition
Statement 1Statement 2Statement 3
………………Statement n
True
False
DoStatement 1Statement 2……………..Statement
mLoop While Condition
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Condition
Statement 1Statement 2Statement 3
………………Statement n
True
FalseDo Until Condition
Statement 1Statement 2……………..Statement
mLoop
Do Until - LoopDo Until - Loop
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Do – Loop UntilDo – Loop Until
Condition
Statement 1Statement 2Statement 3
………………Statement n
True
False
DoStatement 1Statement 2……………..Statement
mLoop Until Condition
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
For counter = start To end [ Step] [ statements 1 ] [ statements 2 ][ statements 2 ]Next [ counter ]
For Next For Next
counter Required in the For statement. Numeric variable. The control variable for the loop. start Required. Numeric expression. The initial value of counter. end Required. Numeric expression. The final value of counter. step Optional. Numeric expression. The amount by which counter is incremented each time through the loop. statements Optional. One or more statements between For and Next that run the specified number of times. Next Required. Terminates the definition of the For loop.
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
For i = 1 To 7 Print “Visual Basic”Next i
i=1 Visual Basici=2 Visual Basici=3 Visual Basici=4 Visual Basici=5 Visual Basici=6 Visual Basici=7 Visual Basic
For - for loop i - use i as our integer1 - start value = 1 To - between start and stop value7 - stop value = 7Next - go to next step
(if i > 7 then end for loop)
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
For i = 0 To 9 Print i Next i i=0
i=1i=2i=3i=4i=5i=6i=7i=8i=9
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Private Sub Command1_Click()Dim i As IntegerDim j As Integer
For i = 1 To 5 For j = 1 To 7 Print i Next jNext i
End Sub
Inner Loop Outer Loop;
12345142172835
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Private Sub Command1_Click()Dim i As IntegerDim j As Integer
For i = 1 To 5 For j = 1 To 7 Print j ; Next j
PrintNext iEnd Sub
Inner Loop Outer Loop
i=1j=1j=2j=3j=4j=5j=6j=7
i=2j=1j=2j=3j=4j=5j=6j=7
i=3j=1j=2j=3j=4j=5j=6j=7
i=4j=1j=2j=3j=4j=5j=6j=7
i=5j=1j=2j=3j=4j=5j=6j=7
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Private Sub Command1_Click()Dim i As IntegerDim j As Integer
For i = 1 To 5 For j = 1 To 7 Print i ; Next j
PrintNext iEnd Sub
Inner Loop Outer Loop
i=1j=1 i=1j=2 i=1j=3 i=1j=4 i=1j=5 i=1j=6 i=1j=7 i=1
i=2j=1 i=2j=2 i=2j=3 i=2j=4 i=2j=5 i=2j=6 i=2j=7 i=2
i=3j=1 i=3j=2 i=3j=3 i=3j=4 i=3j=5 i=3j=6 i=3j=7 i=3
i=4j=1 i=4j=2 i=4j=3 i=4j=4 i=4j=5 i=4j=6 i=4j=7 i=4
i=5j=1 i=5j=2 i=5j=3 i=5j=4 i=5j=5 i=5j=6 i=5j=7 i=5
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
For i = 1 To 10 For j = 1 To 10 Print j ; Next jPrintNext i
For i = 1 To 10 For j = 1 To 10 Print i ; Next jPrintNext i
For i = 1 To 10 For j = 1 To i Print i ; Next jPrintNext i
For i = 1 To 10 For j = 1 To i Print j ; Next jPrintNext i
For i = 1 To 10 For j = i To 10 Print j ; Next jPrintNext i
For i = 1 To 10 For j = i To 10 Print i ; Next jPrintNext i
Palitha Baddegama , Computer Resource Centre, Hingurakgoda
Working with Menus in VB6