1
VB 6.0 /v1.3.2, 2008 // -
VISUAL BASIC 6
1.3.2 e-, -2
http://web.auth.gr/e-topo
2008
2
VB 6.0 /v1.3.2, 2008 // -
3
VB 6.0 /v1.3.2, 2008 // -
.
1 Visual Basic. 5 2 , , , ,
. 20
3 . 62 4 (, , , ).
. 73
5 . 82 6 . 98 7 ( ). 107 8 () . 122 9 Modules, (sub-routines)
(functions). 147
Visual Basic For Applications (VBA) 156 165 DLL 189 197
4
VB 6.0 /v1.3.2, 2008 // -
5
VB 6.0 /v1.3.2, 2008 // -
1 Visual Basic
Visual Basic (VB), Microsoft
Visual Studio,
GW Basic.
GW Basic MS DOS
.
.
Console Applications, ,
, .
VB.
.
, ,
.
. .
VB
( Microsoft)
- (Excel, Word, AutoCAD, Access,
).
GW Basic
Basic A (IBM), Quick Basic
(Microsoft) Turbo Basic (Borland).
Visual Basic VB
, .
start ( windows)
.
6
VB 6.0 /v1.3.2, 2008 // -
- Programs
,
:
,
Microsoft Visual Studio
6.0
MS VB6:
, Visual Basic.
,
,
VB.
7
VB 6.0 /v1.3.2, 2008 // -
Visual Basic VB
.
.
Standard
EXE,
,
.
, , :
1. New:
.
2. Existing:
(/recent,
).
3. Recent:
.
project .
1. project, (module/class module,
[ActiveX], [dll] ).
2. ,
, project ,
, , , .
8
VB 6.0 /v1.3.2, 2008 // -
Standard EXE
VB:
, -.
,
project.
:
9
VB 6.0 /v1.3.2, 2008 // -
ToolBox ( )
.
project.
.
.
. ,
.
.
Properties Window ( )
.
(, ,
),
(
),
, ,
.
,
, (events)
(methods).
10
VB 6.0 /v1.3.2, 2008 // -
1. (Form)
project ,
.
.
. (
) . (
) (.. DLL) .
2.
. ,
(button) ,
, , .
.
3.
,
. , button
move. . .
,
.
11
VB 6.0 /v1.3.2, 2008 // -
Project Explorer ()
O Project Explorer
project
.
, modules,
class modules .
Module
. , ,
. module
, .
Class Module VB. class
module (compilation & build),
DLL.
project. - , DLL (Dynamically Linked Library).
Form Layout ( )
.
.
,
.
12
VB 6.0 /v1.3.2, 2008 // -
Immediate Window ( )
.
(debuging) project
.
Menu ( )
VB.
:
File
Project
Project
project
project ...()
EXE project
(
)
project
VB
13
VB 6.0 /v1.3.2, 2008 // -
Edit
Undo / Redo
14
VB 6.0 /v1.3.2, 2008 // -
View
Immediate Window
Project Explorer
Properties Window
ToolBox
ToolBars
Project
Module
Class Module
Project
(References): project
.
-.
15
VB 6.0 /v1.3.2, 2008 // -
(Components):
ToolBox VB,
, Microsoft, .
.
16
VB 6.0 /v1.3.2, 2008 // -
Project (Project Properties):
project , ,
, .
,
.
Format
2
/
.
17
VB 6.0 /v1.3.2, 2008 // -
Run
/
Tools
/
(Options):
studio Visual Basic. ,
, ,
, .
18
VB 6.0 /v1.3.2, 2008 // -
Help
Microsoft
(Help) Visual Basic .
Visual Studio MSDN.
, , ,
.
Visual Basic.
project.
19
VB 6.0 /v1.3.2, 2008 // -
;
1.
( ) ToolBox.
.
, .
a. .
b. .
c.
.
2. .
().
3. Properties Window
.
1. project ;
2. ;
3. ToolBox;
4. ;
5. project ;
6. ;
7. ;
8. module;
9. project ;
10. ;
11. ;
1. () .
. : 2000,
1500. .
Press me!. .
20
VB 6.0 /v1.3.2, 2008 // -
2 , , , ,
(Controls).
. ,
(Command Button)
( ).
. ;
.
, .
, .
!
, .
21
VB 6.0 /v1.3.2, 2008 // -
,
.
) .
) ( )
.
) ( ,
).
Pointer (): , . , .
.
Label ():
( /
caption).
.
( Properties Window)
.
(Name)
Label.
(
)
Visual Basic.
,
.
Alignment
Label
0 1 2 0:
1:
2:
AutoSize
Label
True False True: Label =
False: Label =
.
BackColor
-
(
-
Label
22
VB 6.0 /v1.3.2, 2008 // -
Label.
)
:
VbRed, vbGreen,
vbCyan, vbYellow,
vbWhite
.
BackStyle
Label
(
)
0 1 0: ()
1: ()
BorderStyle
Label .
0 1 0:
1:
Caption
Label
,
,
.
Label
Caption.
Enabled
Label
.
True False True:
(..
)
False:
.
H Label
.
Font
Caption ()
.
(, )
.
.
ForeColor
.
(
)
:
VbRed, vbGreen,
vbCyan, vbYellow
.
Height
Label twip.
AutoSize
True.
.
.
Label (
)
.
23
VB 6.0 /v1.3.2, 2008 // -
Left
Label
twip.
.
Label
.
MousePointer
Label.
Pointer (
)
Label
.
ToolTipText
Label.
,
,
.
(
/ )
Top
Label
twip.
.
Label
.
Visible
Label
.
True False True:
False:
Width
Label
twip.
AutoSize
True.
.
.
Label (
)
.
WordWrap
Label
.
True False True:
.
False:
Label
.
(reserved word) Visual Basic , ,
, ,
VB . ,
( (Name)
).
Twip Visual Basic
(text width in pixels). ,
,
.
.
24
VB 6.0 /v1.3.2, 2008 // -
(
).
, , .
:
(Sub). ( (Name)
Label1)
Change
Label
( Caption)
Private Sub Label1_Change()
End Sub
Click
Label.
Private Sub Label1_Click()
End Sub
DblClick
Label
Private Sub Label1_DblClick()
End Sub
MouseDown
.
Private Sub Label1_MouseDown(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub
(
)
MouseUp
,
.
Private Sub Label1_MouseUp(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub
MouseMove
Label.
Private Sub Label1_MouseMove(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub
(sub) : (subroutine)
. .
,
(!, Caption),
Click,
.
,
.
25
VB 6.0 /v1.3.2, 2008 // -
,
( .. ,
Label1 ).
, Label,
.
(
Label
Label1)
Move (
)
Label
.
Left, Top, (Width,
Height)
.
Label1.Move 100,200
Label1.Move
100,200,400,400
Refresh
Label
Label1.Refresh
ZOrder
0: ( )
1: ( )
Label1.Zorder = 0
Label1.Zorder = 1
26
VB 6.0 /v1.3.2, 2008 // -
! ! ! ,
, Label,
.
Label1.
Click Label.
Private Sub Label1_Click()
' Hallo Caption
Label1.Caption = Hallo
' ( )
Label1.Left = 500
Label1.Top = 500
' ( )
Label1.Width = 250
Label1.Height = 200
End Sub
' (Remark) Visual Basic
.
. .
REMARKS
.
.
( Label1) Visual Basic
. .
Label1 .
,
:
,
VB!
27
VB 6.0 /v1.3.2, 2008 // -
Frame ():
.
.
,
Label ( )
:
(Name)
Click
Move
AutoSize
DblClick
Refresh
BackColor
MouseDown
ZOrder
BorderStyle
(
)
MouseUp
Caption
MouseMove
Enabled
Font
ForeColor
Height
Left
MousePointer
ToolTipText
Top
Visible
Width
28
VB 6.0 /v1.3.2, 2008 // -
CheckBox ( ):
.
,
CheckBox.
Style
CheckBox.
0: (Standard)
1: (Graphical)
0:
.
1:
Value
CheckBox
.
0: UnChecked
1: Checked
2: Grayed
0:
.
1:
.
2:
(default option)
CheckBox .
(Sub).
( (Name)
Check1)
Click
o
CheckBox. (
,
)
Private Sub Check1_Click()
End Sub
MouseDown
.
Private Sub
Check1_MouseDown (Button
As Integer, Shift As Integer,
X As Single, Y As Single)
End Sub
(
)
MouseUp
,
, .
Private Sub Check1_MouseUp
(Button As Integer, Shift As
Integer, X As Single, Y As
Single)
29
VB 6.0 /v1.3.2, 2008 // -
End Sub
MouseMove
Label.
Private Sub
Check1_MouseMove (Button
As Integer, Shift As Integer,
X As Single, Y As Single)
End Sub
GotFocus
(
Tab Key)
Private Sub
Check1_GotFocus()
End Sub
LostFocus
.
Private Sub
Check1_LostFocus()
End Sub
(label, frame) .
30
VB 6.0 /v1.3.2, 2008 // -
ComboBox ( ):
ComboBox
,
.
(
).
. ,
.
.
(Name), BackColor,
Enabled, Font,
ForeColor, Height,
Left, MousePointer,
Style, ToolTipText,
Top, Visible, Width
Locked
ComboBox
.
True False True:
False:
()
Sorted
ComboBox
.
True False True:
False:
Text
(
.
List
Index:
vName = Combo1.List(1)
ListCount
ComboBox.
iLength = Combo1.ListCount
31
VB 6.0 /v1.3.2, 2008 // -
vName iLength .
,
Visual Basic. To
v i
:
Variant Integer.
.
Variant .
, , , .
Integer
>=0.
.
.
(Sub).
(
(Name)
Combo1)
Change
Combo ( Text)
Private Sub Combo1_Change()
End Sub
Click
Combo.
Private Sub Combo1_Click()
End Sub
DblClick
Combo
Private Sub Combo1_DblClick()
End Sub
GotFocus
(
Tab Key)
Private Sub
Combo1_GotFocus()
End Sub
LostFocus
.
Private Sub
Combo1_LostFocus()
End Sub
Scroll
Private Sub Combo1_Scroll()
End Sub
DropDown
(
) .
Private Sub
Combo1_DropDown()
End Sub
32
VB 6.0 /v1.3.2, 2008 // -
ComboBox
. .
.
(
ComboBox
Combo1)
Clear
ComboBox
Combo1.Clear
AddItem
()
ComboBox
Combo1.AddItem
" 1"
Combo1.AddItem
"12.45"
Combo1.AddItem
RemoveItem
ComboBox
Index:
Combo1.RemoveItem(1)
Combo1.RemoveItem(2)
Refresh
.
Combo1.Refresh
SetFocus
Combo1.SetFocus
33
VB 6.0 /v1.3.2, 2008 // -
HScrollBar ( ), VScrollBar (
) :
.
, . ,
.
.
.
(Name), Enabled,
Height, Left,
MousePointer, Top,
Visible, Width
Min
[-32768, 32767]
Max
[-32768, 32767]
in
Value
[-32768, 32767]
Min
Max
/
SmallChange
(
)
[-32768, 32767]
(
)
LargeChange
(
,
)
[-32768, 32767]
(
)
34
VB 6.0 /v1.3.2, 2008 // -
( )
.
.
(Sub).
( (Name)
Hscroll1) GotFocus, LostFocus Private Sub
HScroll1_GotFocus()
End Sub
Private Sub
HScroll1_LostFocus()
End Sub
Change
.
Private Sub HScroll1_Change()
End Sub
Scroll
.
Private Sub HScroll1_Scroll()
End Sub
(
scrollbar
Hscroll1)
Move, Refresh,
Zorder, SetFocus
(
Hscroll1)
35
VB 6.0 /v1.3.2, 2008 // -
Timer ():
(
), ,
.
.
. . .
Left, Top
(
,
)
Enabled
Enabled
.
,
Enabled
().
True False True
.
Interval
,
.
msec (1000msec=1sec)
0 65535
.
Control Timer :
(Sub).
( (Name)
Timer1) Timer Timer
Enabled = True
Interval.
Private Sub Timer1_Timer()
End Sub
36
VB 6.0 /v1.3.2, 2008 // -
DriveListBox ( ), DirListBox (
), FileListBox ( ):
DriveListBox
.
.
,
.
DirListBox
,
,
.
,
.
FileListBox
(
).
DriveListBox
DirListBox,
.
,
FileListBox.
, ,
,
.
37
VB 6.0 /v1.3.2, 2008 // -
,
.
DriveListBox
(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Drive
.
,
.
,
.
,
:
Drive1.Drive = C
C
SYSTEM
,
.
DirListBox
(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Path
.
,
.
,
.
,
.
FileListBox
(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Path
,
.
,
.
,
,
.
FileName
.
38
VB 6.0 /v1.3.2, 2008 // -
. .
. : Drive1, Dir1
File1: Private Sub Drive1_Change() Dir1.Path = Drive1.Drive File1.Path = Dir1.Path End Sub Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub File1_Click() Dim SingleName As Variant Dim TotalPath As Variant SingleName = File1.FileName TotalPath = Dir1.Path + "\" + File1.FileName End Sub
.
3 blocks Private
Sub End Sub. 3
.
DriveListBox
Drive1. ; Drive1 ,
. ,
.
D, , , DirListBox,
. ,
FileListBox.
, ,
.
()
,
(
).
,
. , .
, - ,
. ,
, .
39
VB 6.0 /v1.3.2, 2008 // -
Dim, ,
. Variant
. ,
,
. ,
(.. MyText.doc).
/ (..
C:\Program Files\MyProgram\Backup\MyText.doc).
, .
,
,
. ,
:
1. ;
;
,
.
( )
,
,
.
DirListBox ( Dir1).
40
VB 6.0 /v1.3.2, 2008 // -
2. ;
, (
Properties Window)
. ,
. :
Private Sub Dir1_Change() Dir1. End Sub
.
,
.
. (
, ).
41
VB 6.0 /v1.3.2, 2008 // -
Shape ():
Shape
.
.
Shape ,
.
.
shape .
(Name), Left,Top,
Width, Height, Visible
BackColor
.
(
)
BackStyle
(
)
0 1 0:
1:
BorderColor
.
(
)
BorderStyle
.
0, 1, 2, 3, 4, 5, 6 0:
1:
2:
3:
4: -
5: -
6:
BorderWidth
1 8192
FillColor
.
(
)
FillStyle
.
0, 1, 2, 3, 4, 5, 6, 7 0:
1:
2:
3:
4:
5:
6:
7:
Shape
0, 1, 2, 3, 4, 5 0:
1:
2:
42
VB 6.0 /v1.3.2, 2008 // -
3:
4:
5:
shape
.
Line ():
Line
Shape.
. 2
(Refresh ZOrder)
.
(Name), BorderColor,
BorderStyle,
BorderWidth, Visible
X1
.
Y1
.
.
X2
.
Y2
.
.
43
VB 6.0 /v1.3.2, 2008 // -
Image ():
.
,
:
(Name), Appearance,
BorderStyle, Enabled,
Height, Width, Left,
Top, Visible
Picture
(
)
.
.
Stretch
()
Image
.
True False True:
.
False:
.
Image
, .
.
.
, ,
PictureBox.
44
VB 6.0 /v1.3.2, 2008 // -
PictureBox ( ):
PictureBox,
Image, ,
.
PictureBox,
,
,
.
,
.
.
(Name), Appearance,
BackColor,
BorderStyle,
DrawStyle,
DrawWidth, Enabled,
FillColor, FillStyle,
Font, ForeColor,
MousePointer, Height,
Width, Left, Top,
Picture, Visible
Align
.
0, 1, 2, 3, 4 0:
1:
2:
3:
4:
AutoRedraw
()
True False True:
.
False:
.
.
AutoSize
True False True:
.
False:
.
45
VB 6.0 /v1.3.2, 2008 // -
.
FontTransparent
.
True False True:
False:
.
ScaleMode
.
0, 1, 2, 3, 4, 5, 6, 7 0:
1: Twip
2: Point
3: Pixel
4:
5:
6:
7:
ScaleLeft
.
.
ScaleTop
.
.
ScaleHeight
.
.
ScaleWidth
.
.
,
.
, ,
.
(0,0).
.
, ,
. Scale
.
Scale .
PictureBox
.
46
VB 6.0 /v1.3.2, 2008 // -
(
PictureBox Picture1)
Move, SetFocus,
ZOrder, Refresh
(
Picure1)
Circle
PictureBox.
,,
R, ,
( )
Picture1.Cirlce (X,Y), R, Color,
Start, End
Cls
PictureBox
Picture1.Cls
Line
PictureBox.
X1,Y1 X2,Y2,
Picture1.Line (X1,Y1)-(X2,Y2),
color
Point
.
(,) TheColor = Picture1.Point
(X,Y)
Pset
.
(,),
Picture1.Pset (X,Y), color
Scale
1:
1:
2:
2:
Picture1. Scale (X1 , Y1)- (X2 ,
Y2)
TextHeight
.
MyH =
Picture1.TextHeight(TheText)
TextWidth
.
MyW =
Picture1.TextWidth(TheText)
,
.
47
VB 6.0 /v1.3.2, 2008 // -
TextBox ( ):
.
.
(Name), Alignment,
Appearance,
BackColor,
BorderStyle, Enabled,
Font, ForeColor,
MousePointer, Height,
Width, Left, Top,
Visible
Locked
.
True False True:
False:
MaxLength
.
.
.
MultiLine
()
True False True:
False:
PasswordChar ,
TextBox
.
.
(1
)
.
ScrollBars
TextBox,
.
0, 1, 2, 3 0:
1:
2:
3:
48
VB 6.0 /v1.3.2, 2008 // -
Text
.
.
.
(Move, Refresh,
Zorder ..) .
, (Click, DoubleClick,
Change, MouseMove ), textbox ,
.
(Sub).
( (Name)
Text1) KeyPress
()
.
Private Sub
Text1_KeyPress(KeyAscii As
Integer)
End Sub
KeyDown
()
.
Private Sub
Text1_KeyDown(KeyCode As
Integer, Shift As Integer)
End Sub
KeyUp (
),
Private Sub
Text1_KeyUp(KeyCode As
Integer, Shift As Integer)
End Sub
49
VB 6.0 /v1.3.2, 2008 // -
CommandButton ( ):
.
.
:
(Name), Appearance,
BackColor, Enabled,
Font, Height, Width,
Left, Top,
ToolTipText,
MousePointer, Visible
Caption
,
.
.
Cancel
[Esc]
.
.
True False True:
[Esc]
False: ,
[Esc]
Default
[Enter]
.
.
True False True:
[Enter]
False: ,
[Enter]
Style
.
0 1 0: (Standard):
.
.
1: (Graphical):
.
,
50
VB 6.0 /v1.3.2, 2008 // -
DisabledPicture
Enabled False
(
)
.
,
.
DownPicture
(
).
.
, .
Picture
.
.
, .
(Move, Refresh, SetFocus, Zorder)
. ,
.
51
VB 6.0 /v1.3.2, 2008 // -
OptionButton ( ):
OptionButton
.
CheckBox
,
checkbox
.
optionbutton
.
;.
,
!
.
(Name), Alignment,
Appearance,
BackColor,
DisabledPicture,
DownPicture, Enabled,
Font, ForeColor,
MousePointer, Left,
Top, Height, Width,
ToolTipText, Visible
Caption
,
.
.
Style
.
0 1 0: (Standard):
.
CommandButton
,
.
52
VB 6.0 /v1.3.2, 2008 // -
.
1: (Graphical):
CommandButton.
.
.
Picture
.
Style
Graphical.
.
,
.
Value
True False True:
.
False:
.
.
53
VB 6.0 /v1.3.2, 2008 // -
:
( ), :
(
, -)
;
:
.
VB
2
. ,
(
, ).
;
!
Frame ().
!
.
2 Frames (
PictureBoxes,
)
Frame
,
:
.
OptionButtons
.
54
VB 6.0 /v1.3.2, 2008 // -
ListBox ( ):
(ComboBox),
,
.
(Name), BackColor,
Enabled, Font,
ForeColor, Height,
Left, MousePointer,
ToolTipText, Top,
Visible, Width
Locked ListBox
.
True False True:
False:
()
Sorted
ListBox
.
True False True:
False:
MultiSelect
0 1 2 0:
1:
.
2:
[Shift] [Ctrl].
Style
.
0 1 0: Standard: (
)
1: CheckBox: (
)
Listndex
0...-
1
(
List1):
AnInteger =
List1.Listindex
ListCount
.
.
(
List1):
AllRegs = List1.ListCount
55
VB 6.0 /v1.3.2, 2008 // -
List
ListIndex
ListIndex.
(
List1):
MyChoice =
List1.List(List1.ListIndex)
ComboBox.
, ListBox Style
CheckBox:
(Sub).
( (Name)
List1) ItemCheck
.
Item
.
Private Sub
List1_ItemCheck(Item As
Integer)
End Sub
56
VB 6.0 /v1.3.2, 2008 // -
Form ():
.
(Project).
,
.
(image), (Label), 3
CommandButtons
.
. ( )
.
.
: , ,
/,
Windows
.
,
.
57
VB 6.0 /v1.3.2, 2008 // -
: ( Caption)
,
,
project.
: ( , ,
) .
,
, .
: .
.
:
.
: ( )
, ...
(Name), Appearance,
AutoRedraw,
BackColor, Caption,
Enabled, FillColor,
FillStyle, Font,
FontTransparent,
ForeColor, Left, Top,
Height, Width,
MousePointer, Picture,
ScaleMode, ScaleLeft,
ScaleHeight,
ScaleWidth, Visible
BorderStyle
,
,
0, 1, 2, 3, 4, 5 0: None:
.
1: Fixed Single:
, ,
.
.
2: Sizable:
58
VB 6.0 /v1.3.2, 2008 // -
.
3: Fixed Dialog:
Fixed Single.
4: Fixed Tool Window:
Light,
Fixed
Single,
.
5: Sizable
ToolWindow:
.
ControlBox
.
True False True:
.
False:
.
Icon
.
Ico, cur
.
.
MaxButton
(
)
True False True:
False:
MinButton
(
)
True False True:
False:
MDIChild
.
.
MDI Form
.
True False True:
MDI Form
False:
Moveable
.
True False True:
.
False:
.
StartUpPosition
.
0, 1, 2, 3 0: Manual:
.
1: CenterOwner:
59
VB 6.0 /v1.3.2, 2008 // -
( MDI
)
2: CenterScreen:
.
3: WindowsDefault:
Windows .
WindowState
.
0, 1, 2 0:
1:
2:
(
Form1)
Circle, Cls, Line, Move,
Point, Pset, Refresh,
Scale, ScaleX, ScaleY,
SetFocus,
TextHeight,
TextWidth, ZOrder
Hide
.
Form1.Hide
PopUpMenu
MENU
MENU
To MENU Form1.PopUpMenu
[]
PrintForm Form1.PrintForm
Show Form1.Show
MENU:
.
. Menu Visual Basic
. Menu
.
.
, .
60
VB 6.0 /v1.3.2, 2008 // -
(Sub).
( (Name)
Form1) Activate
,
( ).
Private Sub Form_Activate()
End Sub
DeActivate
,
.
Private Sub Form_Deactivate()
End Sub
Initialize
.
Private Sub Form_Initialize()
End Sub
Load
.
Private Sub Form_Load()
End Sub
QueryUnload
[x]
.
Private Sub
Form_QueryUnload(Cancel As
Integer, UnloadMode As
Integer)
End Sub
Resize
.
Private Sub Form_Resize()
End Sub
Terminate
project.
.
Private Sub Form_Terminate()
End Sub
Unload
.
Private Sub
Form_Unload(Cancel As
Integer)
End Sub
: ,
, , () Form1.PrintForm
Me.PrintForm
Visual Basic Me
.
61
VB 6.0 /v1.3.2, 2008 // -
;
1. , .
2. o Properties Window. (
) ,
, . .
3. , ,
.
4. ToolBox
.
5.
: . =
1. project ;
2. ;
3. Caption ;
4.
mouse ;
5.
;
6.
;
7. 3
; ;
8.
; ;
9. ;
10. ComboBox ListBox;
11. CheckBox OptionButton;
12. ;
13. ;
14. 12
;
1. project 1 , 1
ComboBox.
ComboBox.
ComboBox
TextBox.
62
VB 6.0 /v1.3.2, 2008 // -
3
ToolBox
MicroSoft
() ,
.
, Menu
standard Visual Basic ( ToolBox),
, .
63
VB 6.0 /v1.3.2, 2008 // -
Menu :
,
VB,
menu
Menu
.
Editor Menu
.
menu .
Caption: menu (.. File, Open,
Print ).
Name: (Name)
.
Shortcut:
menu (.. Open
Ctrl + O).
Checked:
.
.
Enabled: ,
.
.
64
VB 6.0 /v1.3.2, 2008 // -
Visible: ,
.
Next:
.
Insert: .
Delete: .
: *
: *
: .
: .
: .
Cancel: .
Menu .
. , .
Menu
.
65
VB 6.0 /v1.3.2, 2008 // -
(*)
.
.
[]
.
Menu,
.
[] []
- [] 1.
Menu ,
.
, , [e-mail]
[] ()
, [],
. [e-mail] [] 2.
.
[Ctrl+O]
. [Ctrl]
[Shift], [Alt], {F keys} .
, ,
.
Caption - ,
( , )
(
).
66
VB 6.0 /v1.3.2, 2008 // -
[]
.
[].
Caption
.
; , ,
,
:
Private Sub mnuOpen_Click() End Sub
[]
Name mnuOpen.
Menu ,
,
.
67
VB 6.0 /v1.3.2, 2008 // -
Microsoft Common Dialog Control : (
)
Components Microsoft
Common Dialog Control. ;
1 : ToolBox
(pop-up) menu
[Components].
2 : menu Visual Basic ( 1
), [Project] [Components]
Ctrl+T.
,
Common
Dialog.
, ;
. Common Dialog
, /
68
VB 6.0 /v1.3.2, 2008 // -
,
..
o
ToolBox, , (
).
.
( Name) CD1.
ShowOpen : T
.
: CD1.ShowOpen : .
()
, File name, Files of
type.
.
:
69
VB 6.0 /v1.3.2, 2008 // -
DialogTitle: .
: CD1.DialogTitle = FileName: (
).
: CD1.FileName = MyCords.txt Filter: / .
: CD1.Filter = " *.txt (*.txt)|*.txt| *.seq (*.seq)|*.seq"
H CD1.ShowOpen
.
ShowSave :
.
: CD1.ShowSave ( )
.
ShowColor : .
: CD1.ShowColor
70
VB 6.0 /v1.3.2, 2008 // -
Na
(
).
: Color: H
. MyColor = CD1.Color
ShowPrinter :
.
: CD1.ShowPrinter
: Copies: . CD1.Copies = 5
FromPage: . CD1.FromPage = 1
ToPage: .
71
VB 6.0 /v1.3.2, 2008 // -
CD1.ToPage = 12
:
Flags .
CD1.Flags = cdlPDPageNums
ShowFont :
.
:
Flags
:
CD1.Flags = cdlCFBoth
: CD1.ShowFont
: FontName: . yFont = CD1.FontName
Common Dialog
.
72
VB 6.0 /v1.3.2, 2008 // -
;
1. Menu Common Dialog
ToolBox. Menu VB
Common Dialog
.
2. Menu
. .
3. Name Menu
.
4. Common Dialog . ShowOpen
ShowSave
DriveListBox, DirListBox FileListBox.
5. H Flags ( )
ShowFont.
.
1. Menu ;
2. Common Dialog
Scanner ;
3.
;
4. ShowColor a
Menu;
5. Menu;
6. Common Dialog
ShowSave ShowPrint,
Common Dialog ;
1. project
ToolBox ( ). .
Menu 2 , [Show]
[Hide].
(.. Label, CommandButton
). Menu [Show],
, [Hide]
.
73
VB 6.0 /v1.3.2, 2008 // -
4 1) (, , , ) 2)
1)
.
.
,
; .
,
.
.
:
Result = 10 + 15
Result
.
Result = X1 + X4
(
) Result X1, X4 .
1 12 (1 = 12) 4 5.
Result 17.
Result
.
( 1 4).
Result, X1, X4 .
(
). ,
74
VB 6.0 /v1.3.2, 2008 // -
,
() .
Byte 1 byte [0255]
Integer 2 bytes
[-3276832767]
Long 4 bytes
[2147483648...2147483647]
Single 4 bytes
[ 3.402823*1038
1.401298*10-45]
[1.401298*10-45...
3.402823*1038] .
Double 8 bytes
[ 1.79769313486232*10308 ...
4.94065645841247*10-324 ]
[4.94065645841247*10-324
...1.79769313486232*10308]
.
String 1 byte / ,
Variant 16 bytes .
Boolean 1 byte True False
;
(
Visual Basic) .
, .
.
: Dim
:
Dim MyVariable As
:
Dim MyName As Variant
Dim MyCounter As Integer
Dim Ok_Pressed As Boolean
Dim X3 As Double , Y3 As Double , X4 As Double , Y4 As Double
75
VB 6.0 /v1.3.2, 2008 // -
: Dim ,
.
.
Private Sub Command1_Click() Dim MyName As Variant MyName = George Print MyName End Sub
(MyName) ,
, . MyName
.
Private Sub Command2_Click() Print MyName End Sub
: Public
:
Public MyVariable As
:
Public MyName As Variant
Public MyCounter As Integer
Public Ok_Pressed As Boolean
Public X3 As Double, Y3 As Double, X4 As Double, Y4 As Double
: Public Declarations
().
.
,
.
.
public ;
Public
.
76
VB 6.0 /v1.3.2, 2008 // -
,
,
( Form2, Form1) :
Print Form1.MyName
, Public,
(
).
,
.
:
Private Sub Command1_Click() Dim MyName As Variant Dim MyCounter As Integer Dim Pressed As Boolean Dim X3 As Double, Y3 As Double, X4 As Double, Y4 As Double MyName = "George" MyCounter = 12 Pressed = True X3 = 15.3456 Y3 = -24.9912 X4 = (X3 + Y3) / 2 Y4 = X3 - X4 End Sub
True (
False) VB.
MyName
George. ,
VB () George
.
MyCounter, Integer a
George
( !). ;
,
. 12.87
77
VB 6.0 /v1.3.2, 2008 // -
. (
13).
Pressed,
(True False), .
Pressed, =
True False , .
( ) Single Double,
( ).
4 4 .
,
.
:
1. ;
2. ;
(1).
:
) .
) Visual Basic.
,
.
:
Dim vMyName As Variant Dim iMyCounter As Integer Dim boPressed As Boolean Dim dX3 As Double, dY3 As Double, dX4 As Double, dY4 As Double Dim siNumber As Single Dim sNameOfFile As String. Dim lCounter As Long
(2).
. .
Variant ( ), ,
.
78
VB 6.0 /v1.3.2, 2008 // -
( ) Option Explicit
Declarations , .
:
)
( )
= .
1 = 2 * 1.00023 + 2 * 3 / 4 + 55.12
1.
) (
), ,
3 = 3 + 50.412
Visual Basic : 3 50.412
( ).
,
.
1 = 1647.2791
1 = 2 * 1.00023 + (2 * 1 ) / (4 + 55.12 + 1)
79
VB 6.0 /v1.3.2, 2008 // -
2)
,
.
,
.
,
.
,
( / records ),
.
(
!!!).
.
,
, .
.
Public Module. ,
.
65 :
Dim MyAr(65) As Single
. MyAr
. n- , ..
Dim MyAr(65, 5, 4) As Single
, .
Visual Basic
!
Dim MyAr(65, 5, 4, 2, 12) As Single
;
; ,
80
VB 6.0 /v1.3.2, 2008 // -
.
:
Dim dX(50, 3) As Double
dX(1, 2) = 14.455
dX(10, 1) = 15.12
dX(50, 3) = 4.488
Dim.
.
;
, :
Dim MyArray( ) As Single
(ReDim)
MyArray
.
ReDim MyArray(iNumber1, iNumber2) [As Single]
( [ ] )
. ,
.
:
iNumber1 iNumber2.
(Byte, Integer, Long).
81
VB 6.0 /v1.3.2, 2008 // -
;
1. =
( )
.
2.
(met1 = met1 + 1).
3.
, ,
Visual Basic.
4.
,
.
5. Dim
, .
project
Public Declarations
.
6. ,
,
ReDim.
1. 13.176
String; A , ;
2. Variant
;
3.
; ,
;
4.
Boolean;
1. project 2 2 .
2 ,
,
.
Label.
82
VB 6.0 /v1.3.2, 2008 // -
5
Visual Basic
.
.
(intrinsic functions)
VB ,
.
:
) :
Sin (), Cos
(), bs ( ) . :
Sin(a)
Cos(a)
Tan(a)
Atn(a)
Log(a)
>0
Sqr(a)
>=0
Abs(a)
Int(a)
Rnd *
0
Sgn(a)
.
>0 1
=0 0
83
VB 6.0 /v1.3.2, 2008 // -
, .
Microsoft ( VB)
,
:
Atn(a / Sqr(-a * a + 1)) (Arcsin) a = Sin(b)
Atn(-a/Sqr(-a * a + 1)) + 2 * Atn(1) (Arccos) a = Cos(b)
Log(a) / Log(N)
a mod b
a mod b = a int(a/b) * b
Modulo ( ) ,b
:
arcsin, arccos, arctan2 (
) intrinsic functions .
, ..,
2 (
: tanG12 = DX/DY , G12 = arctan [DX/DY] ).
, Arccos
,
Arccos ( Atn ).
()
,
() Fortran Pascal TAN, ATAN,
ATAN2, SIN, ASIN, COS, ACOS (
) : DTA, DATAN, DATAN2,
() Excel MS Office TAN, ATAN,
ATAN2, SIN, ASIN, COS, ACOS . , ()
Excel Fortran Pascal
,
VB.
84
VB 6.0 /v1.3.2, 2008 // -
(Help - F1) Visual Basic
(Derived Math Functions)
:
Function Derived equivalents
Secant Sec(X) = 1 / Cos(X)
Cosecant Cosec(X) = 1 / Sin(X)
Cotangent Cotan(X) = 1 / Tan(X)
Inverse Sine Arcsin(X) = Atn(X / Sqr(-X * X + 1))
Inverse Cosine Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
Inverse Secant Arcsec(X) = Atn(X / Sqr(X * X 1)) + Sgn((X) 1) * (2 * Atn(1))
Inverse Cosecant Arccosec(X) = Atn(X / Sqr(X * X - 1)) + (Sgn(X) 1) * (2 *
Atn(1))
Inverse Cotangent Arccotan(X) = Atn(X) + 2 * Atn(1)
Hyperbolic Sine HSin(X) = (Exp(X) Exp(-X)) / 2
Hyperbolic Cosine HCos(X) = (Exp(X) + Exp(-X)) / 2
Hyperbolic Tangent HTan(X) = (Exp(X) Exp(-X)) / (Exp(X) + Exp(-X))
Hyperbolic Secant HSec(X) = 2 / (Exp(X) + Exp(-X))
Hyperbolic Cosecant HCosec(X) = 2 / (Exp(X) Exp(-X))
Hyperbolic Cotangent HCotan(X) = (Exp(X) + Exp(-X)) / (Exp(X) Exp(-X))
Inverse Hyperbolic
Sine
HArcsin(X) = Log(X + Sqr(X * X + 1))
Inverse Hyperbolic
Cosine
HArccos(X) = Log(X + Sqr(X * X 1))
Inverse Hyperbolic
Tangent
HArctan(X) = Log((1 + X) / (1 X)) / 2
Inverse Hyperbolic
Secant
HArcsec(X) = Log((Sqr(-X * X + 1) + 1) / X)
Inverse Hyperbolic
Cosecant
HArccosec(X) = Log((Sgn(X) * Sqr(X * X + 1) + 1) / X)
Inverse Hyperbolic
Cotangent
HArccotan(X) = Log((X + 1) / (X 1)) / 2
Logarithm to base N LogN(X) = Log(X) / Log(N)
85
VB 6.0 /v1.3.2, 2008 // -
: ( .. Arcsin)
.
.
: Private Sub Command1_Click() Dim dCos As Double, dSin As Double, dAtn As Double Dim siRand As Single, dMyNum As Double dMyNum = 1.3466 dCos = Cos(dMyNum) dSin = Sin(dMyNum) dAtn = Atn(dMyNum) Randomize Timer siRand = Rnd * dMyNum Debug.Print dMyNum Debug.Print dCos, dSin, dAtn Debug.Print siRand End Sub
Command1.
.
dMyNum.
dCos, dSin, dAtn
Cos, Sin Atn ( ),
dMyNum.
Randomize Timer: VB , , Rnd.
Rnd
.
[0...dMyNum),
siRand.
Debug.Print: , Immediate Window ( ).
.
86
VB 6.0 /v1.3.2, 2008 // -
Atn.
Private Sub Command1_Click() Dim Pi As Double Pi = 4 * Atn(1) Debug.Print " = "; Pi End Sub
Immediate Window :
. ,
.
) :
,
, .
String Variant.
Len(aString)
aString
.
().
Mid(aString,
iStartP, iEndP)
aString
iStartP
iEndP.
(),
.
Variant String
87
VB 6.0 /v1.3.2, 2008 // -
Left(aString,
iLength)
aString
(
)
.
(),
()
Variant String
Right(aString,
iLength)
aString
(
)
.
(),
()
Variant String
Trim(aString)
.
().
Ltrim(aString)
.
().
Rtrim(aString)
.
().
88
VB 6.0 /v1.3.2, 2008 // -
: Private Sub Command1_Click() Dim vMyName As Variant Dim iStPos As Integer, iEndPos As Integer Dim iLength As Integer, vNameAfter As Variant vMyName = " Fanis " iLength = Len(vMyName) Debug.Print " = "; vMyName Debug.Print " = "; iLength vMyName = Trim(vMyName) iLength = Len(vMyName) Debug.Print " = "; vMyName Debug.Print " = "; iLength iStPos = 1 iEndPos = 3 vNameAfter = Mid(vMyName, iStPos, iEndPos) Debug.Print " = "; vNameAfter End Sub
:
. vMyName .
vMyName iLength.
.
.
vMyName .
2 ,
(1 3 ).
vNameAfter 3 vMyName
( 1 3). .
.
89
VB 6.0 /v1.3.2, 2008 // -
) :
,
.
Now
.
Date
.
Time
.
Year(aDate)
.
Month(aDate)
.
Day(aDate)
.
Hour(aTime)
.
Minute(aTime)
.
Second(aTime)
.
:
Dim MyDate As Date
, Variant
.
90
VB 6.0 /v1.3.2, 2008 // -
: Private Sub Command1_Click() Dim vMyDate As Variant, AnotherDate As Date vMyDate = Now Debug.Print vMyDate Debug.Print Hour(vMyDate), Minute(vMyDate), Second(vMyDate) AnotherDate = #8/22/2005# Debug.Print AnotherDate End Sub
,
.
) , :
.
MsgBox ( )
:
Prompt : ( /
.
Buttons As VbMsgBoxStyle = vbOKOnly :
MsgBox .
Title : O MsgBox.
91
VB 6.0 /v1.3.2, 2008 // -
HelpFile :
.
Context : Visual Basic
( ).
.
:
vbInformation + vbOKCancel
:
. HelpFile
Context
.
Prompt
.
(Active
Interface).
92
VB 6.0 /v1.3.2, 2008 // -
InputBox ( )
:
InputBox
InputBox. Answer.
Prompt : InputBox
.
Title : O InputBox.
Default : ( ).
XPos :
InputBox.
YPos : InputBox.
HelpFile :
.
Context : Visual Basic
( ).
,
, 145.522.
XPos, YPos HelpFile
Context. XPos, YPos
.
93
VB 6.0 /v1.3.2, 2008 // -
Answer.
InputBox Prompt.
Format
:
:
Format( , )
.. Format(MyNumber, 0,000)
H Format ,
, .
Format ,
.
Private Sub Command1_Click() Dim dNum1 As Double, dNum2 As Double dNum1 = 23.342342 dNum2 = 0.12342342 Debug.Print Format(dNum1, "0.000") Debug.Print Format(dNum1, "0000.0000") Debug.Print Format(dNum1, "-0.0") Debug.Print Format(dNum1, "0.00000000000") Debug.Print Format(dNum2, "0.00%") Debug.Print Format(dNum2 * dNum1 / (dNum2 + dNum1), ".000%") End Sub
94
VB 6.0 /v1.3.2, 2008 // -
:
Format
.
Visual Basic:
95
VB 6.0 /v1.3.2, 2008 // -
Private Sub Form_Load() Dim MyTime As Variant, MyDate As Variant, MyStr As Variant MyTime = #5:04:23 PM# MyDate = #1/27/1993# ' format. MyStr = Format(Time, "Long Time") Debug.Print MyStr ' format. MyStr = Format(Date, "Long Date") Debug.Print MyStr MyStr = Format(MyTime, "h:m:s") ' "17:4:23". Debug.Print MyStr MyStr = Format(MyTime, "hh:mm:ss AMPM") ' "05:04:23 ". Debug.Print MyStr MyStr = Format(MyDate, "dddd, mmm d yyyy") ' ", 27 1993". Debug.Print MyStr ' format MyStr = Format(5459.4, "##,##0.00") ' "5,459.40". Debug.Print MyStr MyStr = Format(334.9, "###0.00") ' "334.90". Debug.Print MyStr MyStr = Format(0.5315, "0.00%") ' "53.15%". Debug.Print MyStr MyStr = Format("HELLO", "") ' "THIS IS IT". Debug.Print MyStr End Sub
Ta :
96
VB 6.0 /v1.3.2, 2008 // -
,
, .
ReMark, Compiler Visual Basic, , . .
97
VB 6.0 /v1.3.2, 2008 // -
;
1. Visual Basic
.
.
2.
.
Visual Basic.
3. Ta
.
4. MsgBox /
.
5. InputBox .
.
6. Format /
.
,
().
1. ;
2.
;
3.
;
4.
;
5. String;
Variant;
6.
MsgBox;
7. InputBox;
8.
InputBox
; .
1. project 3
3 3 .
98
VB 6.0 /v1.3.2, 2008 // -
6
Visual Basic
.
:
2 . , . . /.
,
.
; Visual Basic
:
, .
,
set .
IfThenElse(End If) :
If Then Else
:
MyNum1 = 12
MyNum2 = 23
If MyNum1 > MyNum2 Then MsgBox ! Else
MsgBox O !
;
;
99
VB 6.0 /v1.3.2, 2008 // -
If...Then...Else
.
. ,
.
=
>
<
>=
=<
(
,
).
(argument): , , , , (), ..
Cos(3.14), 3.14
MsgBox !, MsgBox .
Y
.
,
10. ,
:
And ()
Not
Or ()
100
VB 6.0 /v1.3.2, 2008 // -
Xor ()
,
IfThen Block IfEnd
If
If [And ] [r ] Then
Else
End If
, .
:
X1 = 30.45 : Y1 = 23.11
X2 = -49.77 : Y2 = 16.78
Z1 = 100 : Z2 = 0
If X1 > X2 And Y1 < Y2 Then
Z1 = Z2
Else
Z2 = Z1
End If
Debug.Print Z1 , Z2
; : 100 100
If X1 < Abs(X2) And (Y1 - Y2) > 0 Then
Z1 = Z2
Else
Z2 = Z1
End If
Debug.Print Z1 , Z2
; : 0 0
If X1 < X2 Or Y1 = Y2 Then
Z1 = Z2
Else
Z2 = Z1
End If
Debug.Print Z1 , Z2
101
VB 6.0 /v1.3.2, 2008 // -
; : 100 100
If X1 > X2 Xor Y1 > Y2 Then
Z1 = Z2
Else
Z2 = Z1
End If
Debug.Print Z1 , Z2
; : 100 100
(And, Or, Xor, Not) .
Block IfEnd If ElseIf,
Else If.
If X1 > X2 And Y1 < Y2 Then
Z1 = Z2
Debug.Print Z1, Z2
ElseIf X1 > X2 Then
MsgBox " "
ElseIf Y1 < Y2 Then
MsgBox " "
End If
;
. ( 1,1
).
If X1 > X2 And Y1 < Y2 Then 1 >2 1 < 2 :
Z1 = Z2 2 1
Debug.Print Z1, Z2 1 2
ElseIf X1 > X2 Then 1 > 2 :
MsgBox " "
ElseIf Y1 < Y2 Then 1 < 2 :
MsgBox " "
End If Block If
To
.
102
VB 6.0 /v1.3.2, 2008 // -
OptionButton;
Block If.
4 Option Buttons ,
, ,
OptRed, OptGreen, OptBlue, OptCyan, .
. Option Buttons
, Value = True,
( ).
Check!.
:
CommandButton :
Private Sub Command1_Click() If OptRed.Value = True Then MsgBox " !" ElseIf OptGreen.Value = True Then MsgBox " !" ElseIf OptBlue.Value = True Then MsgBox " !" ElseIf OptCyan.Value = True Then MsgBox " !" End If End Sub
(Menu [Run] -> [Start]
F5). Check!.
!
103
VB 6.0 /v1.3.2, 2008 // -
ElseIf
. , Else .
Block IfEnd If,
Visual Basic!
H IfEnd If
.
Block .
Select Case :
Select Case
Case a
...
Case
...
Case
...
End Select
H Select Case ,
, Block .
, , ,
, Is Else.
To (num1 To num2)
Is + / : (.. Is > 23 , Is
104
VB 6.0 /v1.3.2, 2008 // -
Private Sub Command1_Click() Dim i As Integer Randomize Timer i = Int(Rnd * 100) [0,100) Debug.Print "Number i = "; i Select Case i Case 0 To 10 Debug.Print " i [0...10]" Case 11 To 30 Debug.Print " i [11...30]" Case 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 Debug.Print " i [31...50]" Case 51 To 60, 62, 64, 66, 68, 70 Debug.Print " i [51...60] [61...70]" Case Else Debug.Print " i " End Select End Sub
:
Block If, Select Case
Case :
105
VB 6.0 /v1.3.2, 2008 // -
Private Sub Command2_Click() Dim i As Integer Randomize Timer i = Int(Rnd * 100) [0,100) Debug.Print "Number i = "; i Select Case i Case 0 To 65 Debug.Print " i [0...65]" Case 10 To 30 Debug.Print " i [11...30]" Case 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 Debug.Print " i [31...50]" Case Is > 66 Debug.Print " i " End Select End Sub
. ;
i = 21 Case
Block Select Case.
! i=
38, 28, 22.
Case Select Case Block.
106
VB 6.0 /v1.3.2, 2008 // -
;
1. Block IfEnd If
f End If.
2. Block If, .
3. To Select Case Block
Select Case End Select.
4. Select Case Case
.
5. .
6. Else
.
7. ElseIf
.
1. (Block If
Select Case); ;
2. ;
3. Block IfEnd If;
4. Xor;
5. Is;
6. Case;
7. Case Block If...End If;
Block IfEnd If Block Select Case;
1. project OptionButtons
Block IfEnd If, Block Select Case.
, .
2. project
(ComboBox), 6 , 2
Block IfEnd If Block Select Case
.
3. project
10 30.
.
.
,
. ,
. ,
.
107
VB 6.0 /v1.3.2, 2008 // -
7 ( )
Visual Basic
.
. ,
.
:
40 .
40 . ,
.
. ,
.
Visual Basic.
,
,
.
, project
10
( ). 10
. ,
( ), 100 ...!
,
.
108
VB 6.0 /v1.3.2, 2008 // -
For [Step] Next .
:
For = [Step ]
Next
:
Private Sub Command1_Click() Dim i As Integer For i = 1 To 10 1 i 10. Debug.Print i * 10 Next i To i (i+1) End Sub
For i = 1 To 10 10 . ( i), Step ( Next). Step , . Debug.Print i * 10 i 10. Next i Next i ( = 1)
, 10,20,...,90,100.
i 11 For,
, i , .
Next.
,
:
109
VB 6.0 /v1.3.2, 2008 // -
i
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
7 7 70
8 8 80
9 9 90
10 10 100
11 () i 11
)
:
Private Sub Command1_Click() Dim i As Integer For i = 1 To 10 Step 2 1 i 10. Debug.Print i * 10 Next i To i (i+2) End Sub
i
1 1 10
2 3 30
3 5 50
4 7 70
5 9 90
11 () i 11
)
Private Sub Command3_Click() For i = 10 To 3 Step -2 Debug.Print i * 10 Next i End Sub
10
, i 3. i
110
VB 6.0 /v1.3.2, 2008 // -
= 2 ( Next). i 3,
ForNext .
i
1 10 100
2 8 80
3 6 60
4 4 40
2 () i 2 (
111
VB 6.0 /v1.3.2, 2008 // -
i j i , j, i * j * 10
3 1 3 30
2 1 2 20 1
1 1 1 10
3 2 3 60
2 2 2 40 2
1 2 1 20
3 3 3 90
2 3 2 60 3
1 3 1 30
3 4 3 120
2 4 2 80 4
1 4 1 40
O ( j)
i, .
ForNext.
Block IfEnd If .
,
.
300 ,
,
= 100, =200 = 45.
25 ,
. ,
Exit For.
,
ForNext
.
, .
Private Sub Command1_Click() Dim Cords(100, 2) As Single Dim j As Integer, k As Integer For j = 1 To 100 For k = 1 To 2 Randomize Timer Cords(j, k) = Rnd * 200 - 100 Next k Next j End Sub
112
VB 6.0 /v1.3.2, 2008 // -
100 2
. (-
100...100). ;
.
, ,
200 200 !!!
2 (
(n,1) (n,2) 0 20.
1
.
( )
.
Next j
.
For j = 1 To 100 If Cords(j, 1) >= 0 And Cords(j, 1) = 0 And Cords(j, 2) 100 .
>> .
( 20)
>>> .
) j.
) , .
113
VB 6.0 /v1.3.2, 2008 // -
ForNext
.
Private Sub Command1_Click() Dim Gend As Integer, Gstep As Integer, Counter As Integer Gend = 20 Gstep = 1 For Counter = 1 To Gend Step Gstep Gend = 5 Gstep = 2 Debug.Print Counter Next Counter End Sub
Gend = 20 Gstep = 1. Gend Gstep . . Counter: 1, 2, ,19, 20.
( ).
.
.
114
VB 6.0 /v1.3.2, 2008 // -
WhileWend .
:
While
Wend
.
:
Private Sub Command1_Click() Dim x As Single x = 10 While x >= 0 x 0 Debug.Print x x x = x 1 x 1 Wend End Sub
( ). x 1
.
,
(!) :
Private Sub Command1_Click() Dim iRand As Integer, iUser As Integer Randomize Timer iRand = Int(Rnd * 20) + 1 iUser = 0 While iRand iUser iUser = InputBox(" [1...20]", " ") Wend MsgBox "! " & iUser, vbExclamation, "!" End Sub
[1...20].
0, .
115
VB 6.0 /v1.3.2, 2008 // -
. ,
Wend.
!
( ForNext Exit For).
( )
.
,
, . j = 1 While Cords(j, 1) < 0 Or Cords(j, 1) > 20 Or Cords(j, 2) < 0 Or Cords(j, 2) > 20 j = j + 1 Wend MsgBox ", " & j
, ! ( ). j . .
WhileWend. , Microsoft
.
116
VB 6.0 /v1.3.2, 2008 // -
Do While / Until Loop .
:
Do While
[Exit Do]
Loop
(DoLoop).
WhileWend.
Exit Do
(
Exit For).
Do Until
[Exit Do]
Loop
While Until,
.
Do UntilLoop
Private Sub Command1_Click() q = 10 Do Until q >= 30 Debug.Print q q = q + 1 Loop End Sub
H q 30.
q .
,
( ) .
ForNext Next
.
Loop Wend ( )
.
DoLoop :
117
VB 6.0 /v1.3.2, 2008 // -
Do
[Exit Do]
Loop While
Do
[Exit Do]
Loop Until
H
.
DoLoop ,
, o
.
. ,
,
.
Private Sub Command1_Click() Do MyAnswer = InputBox(" ", " ") Loop Until MyAnswer = "LetMeIn!" MsgBox " !" End Sub
Private Sub Command1_Click() Do MyAnswer = InputBox(" ", " ") Loop While MyAnswer "LetMeIn!" MsgBox " !" End Sub
Ta !!!
:
118
VB 6.0 /v1.3.2, 2008 // -
Private Sub Command1_Click() Dim Ar(15) As Single, i As Integer Do i = i + 1 Randomize Timer Ar(i) = Rnd * 100 Debug.Print i, Ar(i) Loop Until i = UBound(Ar, 1) End Sub
Ar
= 15. ,
Ar .
DoLoop Until,
.
, UBound. H Ubound
( 15).
(Ar) .
,
1.
Loop,
, (
Do.
Ubound, ,
LBound
. Uound.
; 1,
2 ;
.
. ,
UBound, LBound.
Dim ... UBound LBound
MyAr(-6 22) 22 -6 22-(-6)=28
MyAr(-6 22, -3 8) 22 8 -6 -3 28 11
119
VB 6.0 /v1.3.2, 2008 // -
: -6 22, -3 8.
,
.
.
120
VB 6.0 /v1.3.2, 2008 // -
;
1. ForNext .
Exit For.
2. WhileWend, Do
WhileLoop, Do UntilLoop DoLoop
While, DoLoop Until. DoLoop
xit Do.
3. ForNext
Next, (Step).
4. WhileWend, Do WhileLoop, Do UntilLoop
,
. , DoLoop While,
DoLoop Until ,
.
5. ForNext m
, ForNext
k ,
m * k.
1.
;
2.
; ;
3.
10
, ,
10 ,
10
;
4. ForNext
m ;
5.
: Dim MyAr(-10 To 10) ;
6.
3 ;
121
VB 6.0 /v1.3.2, 2008 // -
1. project 10
[20...40],
. .
2. project 2 10
,
[0...25].
.
( )
,
.
2 (
).
3. project 20
[0...20].
1, 2, 3,,19,20 .
.
4. project
(m,n) + B(m,n) = C(m,n).
[1...10].
5. project
(m,n) * B(n,k) = C(m,k).
[1...10].
122
VB 6.0 /v1.3.2, 2008 // -
8 ()
/. .. Microsoft Word
,
, , .
.
, ,
.
() .
( )
.
2 : ( /
sequential) (direct, / random) .
123
VB 6.0 /v1.3.2, 2008 // -
( / Sequential files):
,
, , .
. , .
30 ,
29. ,
,
, .
; ,
Text Editor,
NotePad () Windows. ,
. .
.
:
Open ( )
: (1) Open TheFileName For Input As #n
(2) Open TheFileName For Output As #n
(3) Open TheFileName For Append As #n
:
Open D:\MyProgs\Texts\MyText.txt For Input As #1
H TheFileName
Variant String, (
full path name).
n [1...255] ( )
()
Visual Basic .
.
For Input: .
,
, .
For Output: .
, .
124
VB 6.0 /v1.3.2, 2008 // -
, .
.
For Append:
. ,
.
( ,
For Output). , .
,
.
,
.
, FreeFile. H FreeFile
.
( ) : Dim FF As Byte ( Integer) Dim FilNam As Variant FilNam = D:\MyProgs\Texts\MyText.txt FF = FreeFile Open FilNam For Input As #FF
,
.
Open MyFile.txt For Output As #1
MyFile.txt 1
:
1
.
Open MyFile.txt For Input As #1
MyFile.txt 1
:
1
.
MyFile.txt .
Open MyFile.txt For Append As #1
MyFile.txt 1
125
VB 6.0 /v1.3.2, 2008 // -
:
1
.
.
.
Close ( )
: (1) Close #n
(2) Close
:
Close #1
:
1. Close,
.
.
2. ,
.
.
Print ( )
: Print #n, 1, 2 , ...
:
Print #1,
Print #1, , MyAr(1,5), MyCounter, j,
Output Append.
. :
Print #n,
[n] .
[n].
,
, .
126
VB 6.0 /v1.3.2, 2008 // -
: Open MyFile.txt For Output As #6 Print #6, Print #6, Close #6
Input ( )
: Input #n, 1, 2, ...
:
Input #1, Rec1
Input #1, Rec1, MyVariable, TheDate
,
Input. ,
, ,
.
!!!:
,
Input past end of file,
. !
: Open MyFile.txt For Input As #6 Input #6, vA1 Input #6, vA2 Close #6
:
MyFile.txt (
6).
,
.
,
. ,
: Input #6, vA1, vA2
,
,
.
127
VB 6.0 /v1.3.2, 2008 // -
,
.
Line Input.
Line Input ( )
: Line Input #n, 1
:
Line Input #1, Rec1
H Line Input
.
, Input (
Line Input),
.
,
.
.
EOF (End Of File )
: EOF(n)
[n] .
EOF .
. Private Sub Command1_Click() Dim MyVar As Variant Open "Demo.txt" For Input As #3
While Not EOF(3) Line Input #3, MyVar Wend
Close #3 End Sub
While.
3
. ,
, .
128
VB 6.0 /v1.3.2, 2008 // -
WhileWend.
, Not.
While [While Not EOF(n) /. / Wend]
.
(
bytes) LOF(n).
LOF (Length Of File )
: LOF(n)
:
LengthOfMyFile = LOF(1)
H ( Bytes)
1 LengthOfMyFile.
, ,
, .
.
129
VB 6.0 /v1.3.2, 2008 // -
(Output)
1
Private Sub Command1_Click() Dim Myar(20) As Single, i As Integer
For i = 1 To 20 Myar(i) = i * 5 Next i
Open "demo1.txt" For Output As #1
For i = 1 To 20 Print #1, i, Myar(i) Next i
Close #1 End Sub
(Myar) 20 ,
( ).
demo1.txt 1.
.
.
.
2
Private Sub Command1_Click() Dim Pin(10, 10) As Single, i As Integer, j As Integer For i = 1 To 10 For j = 1 To 10 Pin(i, j) = i * j * 3 Next j Next i Open "demo2.txt" For Output As #15 For i = 1 To 10 For j = 1 To 10 Print #15, Pin(i, j), Next j Print #15, "" Next i Close #15 End Sub
130
VB 6.0 /v1.3.2, 2008 // -
(10 10)
ForNext .
. !
Print #15, Pin(i, j),
, .
. For jNext o
For iNext j i .
,
, ,
!
Print #15, ""
.
.
, .
131
VB 6.0 /v1.3.2, 2008 // -
3
.
Microsoft Common Dialog Control ( )
3 . ToolBox
. CD1. ,
(CommandButton)
Command1.
Private Sub Command1_Click() Dim MyFile As Variant, Pinakas(5, 5) As Single, i As Integer, j As Integer With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowSave End With MyFile = CD1.FileName Open MyFile For Output As #1 For i = 1 To 5 For j = 1 To 5 Pinakas(i, j) = i / j Print #1, Format(Pinakas(i, j), "0.0000"), Next j Print #1, "" Next i Close #1 End Sub
, ! Common
Dialog ( ShowSave) ()
.
Block WithEnd With. To Block
, .
With Command1
.Default = True
.SetFocus
.Caption = "Press Me!"
.Left = 0
.Top = 100
End With
Command1.Default = True
Command1.SetFocus
Command1.Caption = "Press Me!"
Command1.Left = 0
Command1.Top = 100
132
VB 6.0 /v1.3.2, 2008 // -
.
.
.
2 ForNext
.
,
. , ,
Format . :
, Common Dialog Control .
133
VB 6.0 /v1.3.2, 2008 // -
(Input)
1
3 . Dim MyFile As Variant, Pinak(50) As Single, i As Integer, j As Integer With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Input As #1 j = 0 While Not EOF(1) j = j +1 Input #1, Pinak(j) Wend Close #1
,
ShowOpen Common Dialog.
50 .
,
.
While Not EOFWend
.
25 (5 * 5),
.
,
format ( ) .
(5 5),
ForNext :
134
VB 6.0 /v1.3.2, 2008 // -
Dim MyFile As Variant, Pinak(5,5) As Single, i As Integer, j As Integer With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Input As #1 For i = 1 To 5 For j = 1 To 5 Input #1, Pinak(i, j) Next j Next i
Close #1
2
,
Line Input.
Dim MyFile As Variant, Pinak(50) As Single, i As Integer, j As Integer With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Input As #1 j = 0 While Not EOF(1) j = j +1 Line Input #1, Pinak(j) Wend Close #1
.
Pinak
. 5
!
135
VB 6.0 /v1.3.2, 2008 // -
3
, Format
:
[0...1], (1...2], (2...3], (3...5]
Private Sub Command1_Click() Dim ByCase(4) As Integer Dim MyFile As Variant, Pinak(50) As Single, i As Integer, j As Integer With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Input As #1 j = 0 While Not EOF(1) j = j + 1 Input #1, Pinak(j) Select Case Pinak(j) Case 0 To 1 ByCase(1) = ByCase(1) + 1 Case 1 To 2 ByCase(2) = ByCase(2) + 1 Case 2 To 3 ByCase(3) = ByCase(3) + 1 Case 3 To 5 ByCase(4) = ByCase(4) + 1 End Select Wend Debug.Print ByCase(1), ByCase(2), ByCase(3), ByCase(4) Close #1 End Sub
136
VB 6.0 /v1.3.2, 2008 // -
.
Select Case. , ,
.
[0...1], (1...2], (2...3], (3...5]. 1 ; ( 2
3); ( 1 Case ).
2 ; !
Select Case, ,
Case .
Case, End Select
.
137
VB 6.0 /v1.3.2, 2008 // -
(Append)
1
,
, .
Private Sub Command1_Click() Dim MyFile As Variant Dim NameAndLastName As Variant With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Append As #4 Do NameAndLastName = InputBox(" ", _ " ") Loop Until NameAndLastName "" Print #4, Now Print #4, NameAndLastName Close #4 MsgBox " " & NameAndLastName & " !", vbInformation, " " End Sub
.
DoLoop Until
.
138
VB 6.0 /v1.3.2, 2008 // -
_ (UnderScore). H
.
,
, _
[Enter]. H Visual Basic
. ,
.
,
,
.
:
George Newcomer .
& MsgBox
,
.
.
(Random Access Files).
139
VB 6.0 /v1.3.2, 2008 // -
( / Direct, / Random access files):
,
5
4 .
. :
1.
, .
.
,
.
2. ,
,
,
. .
3.
, /
.
4.
Text Editor. .
.
5.
.
Format
.
,
Format .
.
;
Visual Basic,
Type. ,
Type Module
(
) .
140
VB 6.0 /v1.3.2, 2008 // -
Visual Basic
Module :
Menu
, () [Project]
[Add Module].
Menu [Project]
.
Open. Module Module1
project , () .
Module; Module .
( ). .
Module
( project ),
.
(
projects),
. ,
. ,
-
.
141
VB 6.0 /v1.3.2, 2008 // -
, Module (
Module1). Module1 modDiloseis (
).
Type:
:
[Public] Type
1 As
2 As
....
N As
End Type
Type (
) (Integer, Single, String ).
,
.
:
Public Type Points ID As Variant X As Double Y As Double Z As Double End Type
modDiloseis Points.
.
Command6 Points!
, MyPoints Points. , ID, X, Y, Z. . , ! MyPoints ( Points) !
142
VB 6.0 /v1.3.2, 2008 // -
. Private Sub Command6_Click() Dim MyPointAr(50) As Points For i = 1 To 50 Randomize Timer MyPointAr(i).ID = i MyPointAr(i).X = Rnd * 500 MyPointAr(i).Y = Rnd * 850 MyPointAr(i).Z = 150 + Rnd * 50 Next i End Sub
;
Points, ForNext
1. 1,2,3,....,49,50
2. , ,
Type .
, .
: Module,
, :
Public (n,m) As
Module.
,
.
Type , Format ,
.
:
Open _ For Random As #1 Len = _
_
_
.
:
: Put
143
VB 6.0 /v1.3.2, 2008 // -
: Put #n, {},
n , {}
.
,
. : . . : Get
: Get #n, {},
n , {}
.
A : (
TypeEnd Type Module )
Public Type Cords PointID As Integer X As Double Y As Double Z As Double End Type
Private Sub Command6_Click() Dim NewPnt As Cords, i As Integer With CD1 .DialogTitle = " " .Filter = *.* (*.*)|*.*| *.dat (*.dat)|*.dat" .FileName = "Myfile.dat" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Random As #1 Len = Len(NewPnt) For i = 1 To 50 Randomize Timer NewPnt.PointID = i NewPnt.X = Rnd * 1000 NewPnt.Y = Rnd * 1000 - 500 NewPnt.Z = 245 + Rnd * 25 Put #1, NewPnt.PointID, NewPnt Next i Close #1 End Sub
144
VB 6.0 /v1.3.2, 2008 // -
, NewPnt
Cords ( Module)
ForNext i ,
, , NewPnt.
, bytes
NewPnt.
.
Put,
(NewPnt.PointID),
NewPnt.
.
:
Private Sub Command7_Click() Dim GivePoint As Cords, TheCode As Integer With CD1 .DialogTitle = " " .Filter = *.* (*.*)|*.*| *.dat (*.dat)|*.dat" .FileName = "Myfile.dat" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Random As #1 Len = Len(GivePoint) TheCode = InputBox(" ; [1...50]", _ " ") Get #1, TheCode, GivePoint MsgBox "X = " & GivePoint.X & " Y = " & GivePoint.Y & _ " Z = " & GivePoint.Z, vbInformation, " " & TheCode & " " Close #1 End Sub
MsgBox.
,
,
.
( ).
145
VB 6.0 /v1.3.2, 2008 // -
;
1. ,
.
, .
2. ,
n- ,
.
3.
.
.
4.
format ,
, format
.
5. Print # Input #
Put #
Get #.
6. [Public] TypeEnd Type
Module.
1.
;
2. ;
3.
;
4. ;
5. ;
6.
;
7. TypeEnd Type;
8. Modules;
9. ;
10.
Text Editor;
11. _ & ;
12. EOF(n) LOF(n);
13. Line Input # ; ;
14. Close
;
146
VB 6.0 /v1.3.2, 2008 // -
1. project 30
[0...100] . .
,
50.
2. project 30
[0...100] . .
,
,
.
3. 40 [1...40]
, 25 [1...25]
. .
.
10 (
[1...20].
4. 50 (, ,
, ). .
.
20 .
5. Customer ID, Name,
LastName, Tel. 5
.
Label (
) ID .
6. 100
Human ID, Age,
Weight, Height, Sex. .
,
.
147
VB 6.0 /v1.3.2, 2008 // -
9 Modules, (sub-routines), (functions)
! ,
, ,
, ,
, (,
, ) . -
.
:
With CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen End With MyFile = CD1.FileName
,
; !
;
, .
, .
. ,
.
148
VB 6.0 /v1.3.2, 2008 // -
(Sub-Routines)
.
Module.
,
.
,
Module.
.
,
Module. ;
Module, project.
Menu [Project] >> [Add Module] [Existing]
Module
project project .
149
VB 6.0 /v1.3.2, 2008 // -
, .
Module .
:
Private Sub DoThis() ' End Sub
Public Sub DoThat() ' End Sub
DoThis
DoThat . DoThis
Module DoThat (
, Module ).
Module Module1,
. Module1
MyFile .
Public MyFile As Variant
Public Sub OpFile() With Form1.CD1 .DialogTitle = " " .Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat" .FileName = "Myfile.txt" .ShowOpen MyFile = .FileName End With End Sub
Common Dialog CD1,
.
150
VB 6.0 /v1.3.2, 2008 // -
Module ,
.
:
Private Sub Command6_Click() Dim NewPnt As Cords, i As Integer With CD1 .DialogTitle = " " .Filter = *.* (*.*)|*.*| *.dat (*.dat)|*.dat" .FileName = "Myfile.dat" .ShowOpen End With MyFile = CD1.FileName Open MyFile For Random As #1 Len = Len(NewPnt) For i = 1 To 50 Randomize Timer NewPnt.PointID = i NewPnt.X = Rnd * 1000 NewPnt.Y = Rnd * 1000 - 500 NewPnt.Z = 245 + Rnd * 25 Put #1, NewPnt.PointID, NewPnt Next i Close #1 End Sub
Private Sub Command6_Click() Dim NewPnt As Cords, i As Integer Module1.OpFile Open Module1.MyFile For Random As #1 Len = Len(NewPnt) For i = 1 To 50 Randomize Timer NewPnt.PointID = i NewPnt.X = Rnd * 1000 NewPnt.Y = Rnd * 1000 - 500 NewPnt.Z = 245 + Rnd * 25 Put #1, NewPnt.PointID, NewPnt Next i Close #1 End Sub
,
Module1.OpFile .
(
) Module
,
.
OpFile
Module1,
151
VB 6.0 /v1.3.2, 2008 // -
MyFile, Module1 (
Common Dialog
Module1).
O .
,
.
2 .
Module1.
Public Sum As Double
Public Sub DoSum(num1 As Do
Top Related