You are on page 1of 203

1

VISUAL BASIC 6

1.3
e-, -2, 2003-2006
http://web.auth.gr/e-topo

2006
VB 6.0 v1.3 / e- / / -

1
2
3
4
5
6
7
8
9

Visual Basic.
, , , ,
.
.
(, , , ).
.
.
.
( ).
() .
Modules, (sub-routines)
(functions).

3
18
60
71
80
96
105
120
145

Visual Basic For Applications (VBA)



DLL

154
163
187
195

VB 6.0 v1.3 / e- / / -

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)
.

VB 6.0 v1.3 / e- / / -


- Programs
,


:

,
Microsoft Visual Studio
6.0
MS VB6:

Visual Basic.
,
,

VB.

VB 6.0 v1.3 / e- / / -

Visual Basic
VB
.


Standard
EXE,

, , :
1. New:
.
2. Existing:
(/recent,
).
3. Recent:
.
project .

1. project, (module/class module,


[ActiveX], [dll] ).
2. ,
, project ,
, , , .
VB 6.0 v1.3 / e- / / -

Standard EXE
VB:

, -.
,
project.
:

VB 6.0 v1.3 / e- / / -

ToolBox ( )
.

project.
.

.

. ,
.
.

Properties Window ( )

.

(, ,
),
(
),
, ,
.
,
, (events)
(methods).

VB 6.0 v1.3 / e- / / -

1. (Form)
project ,
.


.
. (
) . (
) (.. DLL) .
2.
. ,
(button) ,
, , .

.
3.
,
. , button
move. . .
,
.

VB 6.0 v1.3 / e- / / -

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 ( )

.


.
,
.

VB 6.0 v1.3 / e- / / -

10

Immediate Window ( )


.

(debuging) project

.
Menu ( )


VB.
:
File
Project
Project

project
project ...()

EXE project
(
)
project
VB

VB 6.0 v1.3 / e- / / -

11

Edit
Undo / Redo

VB 6.0 v1.3 / e- / / -

12

View

Immediate Window

Project Explorer
Properties Window

ToolBox

ToolBars
Project

Module
Class Module


Project

(References): project
.
-.

VB 6.0 v1.3 / e- / / -

13

(Components):
ToolBox VB,
, Microsoft, .

.

VB 6.0 v1.3 / e- / / -

14

Project (Project Properties):


project , ,
, .
,
.

Format

2
/
.


VB 6.0 v1.3 / e- / / -

15

Run
/

Tools

(Options):
studio Visual Basic. ,
, ,
, .

VB 6.0 v1.3 / e- / / -

16

Help

Microsoft

(Help) Visual Basic .


Visual Studio MSDN.
, , ,
.

Visual Basic.

project.

VB 6.0 v1.3 / e- / / -

17

;
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!. .

VB 6.0 v1.3 / e- / / -

18

2
, , , ,

(Controls).

. ,
(Command Button)
( ).

. ;
.
, .
, .
!

, .

VB 6.0 v1.3 / e- / / -

19

,

.
) .
) ( )
.
) ( ,
).

Pointer (): ,
. , .
.

Label ():



( /
caption).


.
( Properties Window)
.

(Name)



Label.

Alignment



Label

Label

AutoSize

BackColor



(

)


Visual Basic.
012


,

.

True False

0:
1:
2:
True: Label =

False: Label =
.

Label

VB 6.0 v1.3 / e- / / -

20

Label.

BackStyle

BorderStyle

Caption


Label
(
)



Label .

Label

Enabled


Label
.

Font





Caption ()

ForeColor

Height


Label twip.


AutoSize
True.


)
:
VbRed, vbGreen,
vbCyan, vbYellow,
vbWhite
01

0: ()
1: ()

01

0:
1:

,
,


.
True False

Label

Caption.




.


(, )

.

(

)
:
VbRed, vbGreen,
vbCyan, vbYellow

.
.

True:

(..

)
False:
.
H Label


Label (

)
.

VB 6.0 v1.3 / e- / / -

21

Left


Label

twip.

MousePointer





Label.





Label.

Label

twip.

Label
.

Label
twip.


AutoSize
True.

Label


.


Pointer (

)

ToolTipText

Top

Visible

Width

WordWrap

,
,


.

.

True False

Label



.


Label

.



(
/ )
Label


.
True:
False:


.
.


Label (

)
.

True False

True:
.
False:

Label
.

(reserved word) Visual Basic , ,


, ,
VB . ,


( (Name)
).
Twip Visual Basic
(text width in pixels). ,
,
.
.

VB 6.0 v1.3 / e- / / -

22

(
).
, , .
:

Change


Click


Label
( Caption)

Label.


DblClick


Label



MouseDown


(
)
MouseUp


MouseMove



,
.


Label.


(Sub).
( (Name)
Label1)
Private Sub Label1_Change()
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label1_DblClick()
End Sub
Private Sub Label1_MouseDown(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub
Private Sub Label1_MouseUp(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub
Private Sub Label1_MouseMove(Button As
Integer, Shift As Integer, X As Single, Y As
Single)
End Sub

(sub) : (subroutine)

. .
,
(!, Caption),
Click,
.
,
.

VB 6.0 v1.3 / e- / / -

23


,

( .. ,
Label1 ).

, Label,
.

Move

Refresh
ZOrder

)
Label
.

Label


Left, Top, (Width,


Height)
.

(
Label
Label1)
Label1.Move 100,200

Label1.Move
100,200,400,400
Label1.Refresh

0: ( )
1: ( )

Label1.Zorder = 0
Label1.Zorder = 1

VB 6.0 v1.3 / e- / / -

24

! ! !
,
, 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!

VB 6.0 v1.3 / e- / / -

25

Frame ():


.



.
,
Label ( )
:

(Name)
AutoSize
BackColor

BorderStyle

Caption


Click

DblClick

MouseDown

(
)
MouseUp

MouseMove

Move
Refresh
ZOrder

Enabled
Font
ForeColor
Height
Left
MousePointer
ToolTipText
Top
Visible
Width

VB 6.0 v1.3 / e- / / -

26

CheckBox ( ):




.

,
CheckBox.

Style



CheckBox.

0: (Standard)
1: (Graphical)

Value


CheckBox
.

0: UnChecked
1: Checked
2: Grayed

0:
.
1:



0:
.
1:
.
2:

(default option)

CheckBox .


Click

MouseDown


(
)
MouseUp

o
CheckBox. (
,
)

.

,

, .



(Sub).
( (Name)
Check1)
Private Sub Check1_Click()
End Sub
Private Sub
Check1_MouseDown (Button
As Integer, Shift As Integer,
X As Single, Y As Single)
End Sub
Private Sub Check1_MouseUp
(Button As Integer, Shift As
Integer, X As Single, Y As
Single)

VB 6.0 v1.3 / e- / / -

27


MouseMove



Label.


GotFocus


(

Tab Key)

.


LostFocus

End Sub
Private Sub
Check1_MouseMove (Button
As Integer, Shift As Integer,
X As Single, Y As Single)
End Sub
Private Sub
Check1_GotFocus()
End Sub
Private Sub
Check1_LostFocus()
End Sub


(label, frame) .

VB 6.0 v1.3 / e- / / -

28

ComboBox ( ):

ComboBox
,

.
(
).

. ,
.
.

(Name), BackColor,
Enabled, Font,
ForeColor, Height,
Left, MousePointer,
Style, ToolTipText,
Top, Visible, Width
Locked

Sorted

Text

List

ListCount


ComboBox
.


ComboBox
.

(

True False

True:
False:
()
True:
False:



ComboBox.

True False

Index:


vName =
Combo1.List(1)

iLength =
Combo1.ListCount

VB 6.0 v1.3 / e- / / -

29

vName iLength .
,
Visual Basic. To
v i
:
Variant Integer.
.
Variant .
, , , .
Integer
>=0.
.
.

Change


Combo ( Text)


Click


Combo.


DblClick


Combo


GotFocus


LostFocus


(

Tab Key)

.

Scroll

DropDown

(
) .



(Sub).
(
(Name)
Combo1)
Private Sub Combo1_Change()
End Sub
Private Sub Combo1_Click()
End Sub
Private Sub Combo1_DblClick()
End Sub
Private Sub
Combo1_GotFocus()
End Sub
Private Sub
Combo1_LostFocus()
End Sub
Private Sub Combo1_Scroll()
End Sub
Private Sub
Combo1_DropDown()
End Sub

VB 6.0 v1.3 / e- / / -

30

ComboBox
. .
.

Clear



ComboBox

()
ComboBox

RemoveItem



ComboBox

Refresh

Index:


AddItem

SetFocus

(
ComboBox
Combo1)
Combo1.Clear

Combo1.AddItem
" 1"
Combo1.AddItem
"12.45"
Combo1.AddItem
"mail@mail.com"
Combo1.RemoveItem(1)
Combo1.RemoveItem(2)

Combo1.Refresh
Combo1.SetFocus

VB 6.0 v1.3 / e- / / -

31

HScrollBar ( ), VScrollBar (
) :


.
, . ,
.
.
.

(Name), Enabled,
Height, Left,
MousePointer, Top,
Visible, Width
Min
Max


[-32768, 32767]

[-32768, 32767]
in

[-32768, 32767]
Min
Max

[-32768, 32767]


[-32768, 32767]

(


)

Value

SmallChange

(
)


(

,
)

LargeChange

(


)

VB 6.0 v1.3 / e- / / -

32

( )
.

.

GotFocus, LostFocus



(Sub).
( (Name)
Hscroll1)
Private Sub
HScroll1_GotFocus()
End Sub
Private Sub
HScroll1_LostFocus()

Change

Scroll




.


.

End Sub
Private Sub HScroll1_Change()
End Sub
Private Sub HScroll1_Scroll()
End Sub

Move, Refresh,
Zorder, SetFocus

(
scrollbar
Hscroll1)

(
Hscroll1)

VB 6.0 v1.3 / e- / / -

33

Timer ():


(
), ,

.
.

. . .

Left, Top

Enabled

Interval

(
,


)

Enabled
.
,

Enabled


().

,



.


msec (1000msec=1sec)

True False

True



.

0 65535

Control Timer :

Timer

Timer
Enabled = True


Interval.



(Sub).
( (Name)
Timer1)
Private Sub Timer1_Timer()
End Sub

VB 6.0 v1.3 / e- / / -

34

DriveListBox ( ), DirListBox (
), FileListBox ( ):
DriveListBox



.
.

,
.
DirListBox

,
,

.


,

.
FileListBox
(
).
DriveListBox

DirListBox,

.

,
FileListBox.
, ,
,
.

VB 6.0 v1.3 / e- / / -

35

,
.
DriveListBox

(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Drive

DirListBox

(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Path

FileListBox



.

,

.

.
,
:
Drive1.Drive = C


C

SYSTEM




,

.



.

,

.


,
.




,

.

(Name), BackColor,
Enabled, ForeColor,
Font, Height, Width,
Left, Top,
MousePointer, Visible
Path


,


.


,
.

FileName

,


,

.

.

VB 6.0 v1.3 / e- / / -

36


. : 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.
, ,
.
()
,
(
).
,
. , .
, - ,
. ,
, .

VB 6.0 v1.3 / e- / / -

37

Dim, ,
. Variant
. ,
,
. ,
(.. MyText.doc).
/ (..
C:\Program Files\MyProgram\Backup\MyText.doc).
, .
,
,
. ,
:
1. ;
;

,

.
( )
,
,
.
DirListBox ( Dir1).

VB 6.0 v1.3 / e- / / -

38

2. ;
, (
Properties Window)
. ,
. :
Private Sub Dir1_Change()
Dir1.
End Sub

,
.
. (

, ).

VB 6.0 v1.3 / e- / / -

39

Shape ():
Shape

.


.
Shape ,
.
.
shape .

(Name), Left,Top,
Width, Height, Visible
BackColor



.

(
)


.


.


(
)
01

1 8192

BackStyle

BorderColor

BorderStyle

BorderWidth
FillColor

FillStyle

Shape


(
)
0, 1, 2, 3, 4, 5, 6


(
)
0, 1, 2, 3, 4, 5, 6, 7

0, 1, 2, 3, 4, 5

0:
1:

0:
1:
2:
3:
4: -
5: -
6:

0:
1:
2:
3:
4:
5:
6:
7:
0:
1:
2:

VB 6.0 v1.3 / e- / / -

40

3:
4:

5:

shape
.

Line ():
Line
Shape.
. 2
(Refresh ZOrder)

.

(Name), BorderColor,
BorderStyle,
BorderWidth, Visible
X1
Y1
X2
Y2


.

.

.

.

VB 6.0 v1.3 / e- / / -

41

Image ():


.

,
:

(Name), Appearance,
BorderStyle, Enabled,
Height, Width, Left,
Top, Visible
Picture

Stretch



(
)


()
Image
.




.
True False

True:
.
False:

.
Image

, .

.
.
, ,
PictureBox.

VB 6.0 v1.3 / e- / / -

42

PictureBox ( ):
PictureBox,
Image, ,

.



PictureBox,
,
,


.
,
.

.

(Name), Appearance,
BackColor,
BorderStyle,
DrawStyle,
DrawWidth, Enabled,
FillColor, FillStyle,
Font, ForeColor,
MousePointer, Height,
Width, Left, Top,
Picture, Visible
Align

AutoRedraw

AutoSize



.



()

0, 1, 2, 3, 4

0:
1:
2:
3:
4:

True False

True False

True:
.
False:
.


.
True:
.
False:
.

VB 6.0 v1.3 / e- / / -

43

FontTransparent


.




.

True False

ScaleMode

0, 1, 2, 3, 4, 5, 6, 7

ScaleLeft



.


.



.



.

ScaleTop

ScaleHeight

ScaleWidth


.

.

True:

False:

.
0:
1: Twip
2: Point
3: Pixel
4:
5:
6:
7:

, ,
.
(0,0).
.
, ,
. Scale
.
Scale .

PictureBox

.

VB 6.0 v1.3 / e- / / -

44

Move, SetFocus,
ZOrder, Refresh

Circle


PictureBox.

Cls


PictureBox


PictureBox.


.


.

,,
R, ,
( )

Line
Point

Pset

Scale

TextHeight

TextWidth




.



.

(
PictureBox Picture1)
(

Picure1)
Picture1.Cirlce (X,Y), R, Color,
Start, End
Picture1.Cls

X1,Y1 X2,Y2,

(,)

Picture1.Line (X1,Y1)-(X2,Y2),
color
TheColor = Picture1.Point
(X,Y)

(,),

Picture1.Pset (X,Y), color

1:
1:
2:
2:

Picture1. Scale (X1 , Y1)- (X2 ,


Y2)

MyW =
Picture1.TextWidth(TheText)

MyH =
Picture1.TextHeight(TheText)

,
.

VB 6.0 v1.3 / e- / / -

45

TextBox ( ):




.

(Name), Alignment,
Appearance,
BackColor,
BorderStyle, Enabled,
Font, ForeColor,
MousePointer, Height,
Width, Left, Top,
Visible
Locked

MaxLength

MultiLine

PasswordChar

ScrollBars



.



.

True False

True:
False:




()
,


TextBox
.

.


TextBox,





.

True False





.
True:
False:

(1
)

0, 1, 2, 3

0:

1:
2:
3:

VB 6.0 v1.3 / e- / / -

46

Text


.



.

(Move, Refresh,
Zorder ..) .
, (Click, DoubleClick,
Change, MouseMove ), textbox ,
.

KeyPress


()
.

KeyDown


()

.

KeyUp

(

),



(Sub).
( (Name)
Text1)
Private Sub
Text1_KeyPress(KeyAscii As
Integer)
End Sub
Private Sub
Text1_KeyDown(KeyCode As
Integer, Shift As Integer)
End Sub
Private Sub
Text1_KeyUp(KeyCode As
Integer, Shift As Integer)
End Sub

VB 6.0 v1.3 / e- / / -

47

CommandButton ( ):



.

.
:

(Name), Appearance,
BackColor, Enabled,
Font, Height, Width,
Left, Top,
ToolTipText,
MousePointer, Visible
Caption

Cancel

Default

Style


,


.



[Esc]
.


.



[Enter]
.


.


.





.
True:
[Esc]

False: ,

[Esc]
True:
[Enter]

False: ,

[Enter]
0: (Standard):

.


.
1: (Graphical):


.

,

True False

True False

01

VB 6.0 v1.3 / e- / / -

48

DisabledPicture

DownPicture

Picture





Enabled False
(

)



(
).



.



,
.



, .


, .

(Move, Refresh, SetFocus, Zorder)


. ,
.

VB 6.0 v1.3 / e- / / -

49

OptionButton ( ):
OptionButton


.
CheckBox
,

checkbox

.
optionbutton
.

;.

,
!
.

(Name), Alignment,
Appearance,
BackColor,
DisabledPicture,
DownPicture, Enabled,
Font, ForeColor,
MousePointer, Left,
Top, Height, Width,
ToolTipText, Visible
Caption

Style


,


.


.





.
0: (Standard):

.

CommandButton

,
.

01

VB 6.0 v1.3 / e- / / -

50

Picture

Value




.


Style
Graphical.



True False







.
1: (Graphical):



CommandButton.





.



.

,
.

True:
.
False:
.

VB 6.0 v1.3 / e- / / -

51

:

( ), :
(
, -)
;
:

.
VB
2

. ,

(
, ).
;
!

Frame ().
!

.
2 Frames (
PictureBoxes,
)
Frame
,
:

.

OptionButtons



.

VB 6.0 v1.3 / e- / / -

52

ListBox ( ):


(ComboBox),


,
.

(Name), BackColor,
Enabled, Font,
ForeColor, Height,
Left, MousePointer,
ToolTipText, Top,
Visible, Width
Locked

ListBox

.


ListBox
.


True False

True:
False:
()
True:
False:

012

0:

1:
.
2:

[Shift] [Ctrl].

Style

01

Listndex

0...1

ListCount

0: Standard: (

)
1: CheckBox: (

)
(

List1):
AnInteger =
List1.Listindex
(

List1):
AllRegs = List1.ListCount

Sorted

MultiSelect

True False

VB 6.0 v1.3 / e- / / -

53

List




ListIndex


ListIndex.

(

List1):
MyChoice =
List1.List(List1.ListIndex)

ComboBox.
, ListBox Style
CheckBox:

ItemCheck


.
Item

.



(Sub).
( (Name)
List1)
Private Sub
List1_ItemCheck(Item As
Integer)
End Sub

VB 6.0 v1.3 / e- / / -

54

Form ():

.


(Project).
,
.


(image), (Label), 3
CommandButtons

.


. ( )
.
.
: , ,
/,
Windows
.
,
.

VB 6.0 v1.3 / e- / / -

55

: ( 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:

VB 6.0 v1.3 / e- / / -

56

ControlBox

True False

Icon


(
)


(
)


.




.
MDI Form

.



.



.

Ico, cur
.
True False

MaxButton

MinButton

MDIChild

Moveable

StartUpPosition


.
3: Fixed Dialog:

Fixed Single.
4: Fixed Tool Window:

Light,
Fixed
Single,
.
5: Sizable
ToolWindow:



.
True:
.
False:
.
.
True:
False:

True False

True:
False:

True False

True:
MDI Form
False:

True False

True:
.
False:
.
0: Manual:

.
1: CenterOwner:

0, 1, 2, 3

VB 6.0 v1.3 / e- / / -

57

WindowState

0, 1, 2


( MDI
)
2: CenterScreen:
.
3: WindowsDefault:

Windows .
0:

1:
2:

Circle, Cls, Line, Move,


Point, Pset, Refresh,
Scale, ScaleX, ScaleY,
SetFocus,
TextHeight,
TextWidth, ZOrder
Hide
PopUpMenu

PrintForm
Show

(

Form1)


.

MENU
MENU

Form1.Hide

To MENU

Form1.PopUpMenu
[]

Form1.PrintForm
Form1.Show

MENU:
.
. Menu Visual Basic
. Menu
.

.
, .

VB 6.0 v1.3 / e- / / -

58

Activate

DeActivate

Initialize


,


( ).

,

.

.

Load

QueryUnload


[x]
.

Resize

Terminate

Unload



.

project.


.

.



(Sub).
( (Name)
Form1)
Private Sub Form_Activate()
End Sub

Private Sub Form_Deactivate()


End Sub
Private Sub Form_Initialize()
End Sub
Private Sub Form_Load()
End Sub
Private Sub
Form_QueryUnload(Cancel As
Integer, UnloadMode As
Integer)
End Sub
Private Sub Form_Resize()
End Sub
Private Sub Form_Terminate()
End Sub

Private Sub
Form_Unload(Cancel As
Integer)
End Sub

:
,
, , ()
Form1.PrintForm


Me.PrintForm

Visual Basic Me
.

VB 6.0 v1.3 / e- / / -

59

;
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.

VB 6.0 v1.3 / e- / / -

60

ToolBox
MicroSoft
() ,
.
, Menu
standard Visual Basic ( ToolBox),
, .

VB 6.0 v1.3 / e- / / -

61

Menu :


,
VB,

menu



Menu
.
Editor Menu


.






menu .

Caption: menu (.. File, Open,


Print ).
Name: (Name)
.
Shortcut:
menu (.. Open
Ctrl + O).
Checked:
.
.
Enabled: ,
.
.

VB 6.0 v1.3 / e- / / -

62

Visible: ,
.
Next:
.
Insert: .
Delete: .
: *
: *
: .
: .
: .
Cancel: .
Menu .
. , .
Menu
.

VB 6.0 v1.3 / e- / / -

63

(*)

.



.
[]

.




Menu,

.
[] []
- [] 1.
Menu ,
.
, , [e-mail]
[] ()
, [],
. [e-mail] [] 2.
.
[Ctrl+O]
. [Ctrl]
[Shift], [Alt], {F keys} .
, ,
.
Caption - ,
( , )
(
).

VB 6.0 v1.3 / e- / / -

64


[]
.

[].

Caption




.

; , ,
,
:
Private Sub mnuOpen_Click()
End Sub

[]
Name mnuOpen.
Menu ,
,
.

VB 6.0 v1.3 / e- / / -

65

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

, /

VB 6.0 v1.3 / e- / / -

66

,
..
o
ToolBox, , (
).

.
( Name) CD1.
ShowOpen : T
.
: CD1.ShowOpen
: .

()
, File name, Files of
type.
.

VB 6.0 v1.3 / e- / / -

67

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

VB 6.0 v1.3 / e- / / -

68

Na
(
).

:
Color: H
.
MyColor = CD1.Color

ShowPrinter :
.

: CD1.ShowPrinter
:
Copies: .
CD1.Copies = 5

FromPage: .
CD1.FromPage = 1

ToPage: .

VB 6.0 v1.3 / e- / / -

69

CD1.ToPage = 12

:
Flags .
CD1.Flags = cdlPDPageNums

ShowFont :
.

:
Flags
:
CD1.Flags = cdlCFBoth

: CD1.ShowFont
:
FontName: .
yFont = CD1.FontName

Common Dialog
.

VB 6.0 v1.3 / e- / / -

70

;
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]
.

VB 6.0 v1.3 / e- / / -

71

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 .
(
). ,

VB 6.0 v1.3 / e- / / -

72

,
() .

Byte
Integer

1 byte
2 bytes

Long

4 bytes

Single

4 bytes

Double

8 bytes

String

1 byte /

Variant
Boolean

16 bytes
1 byte


[0255]

[-3276832767]

[2147483648...2147483647]

[ 3.402823*1038
1.401298*10-45]

[1.401298*10-45...
3.402823*1038] .

[ 1.79769313486232*10308 ...
4.94065645841247*10-324 ]

[4.94065645841247*10-324
...1.79769313486232*10308]
.
,

.
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

VB 6.0 v1.3 / e- / / -

73

: 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
.
VB 6.0 v1.3 / e- / / -

74

,
,
( Form2, Form1) :
Print Form1.MyName
, Public,

(
).
,
.
:
Private Sub Command1_Click()
Dim
Dim
Dim
Dim

MyName As Variant
MyCounter As Integer
Pressed As Boolean
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

VB 6.0 v1.3 / e- / / -

75

. (
13).
Pressed,
(True False), .
Pressed, =
True False , .

( ) Single Double,

( ).
4 4 .

,
.

:
1. ;
2. ;
(1).
:
) .
) Visual Basic.
,
.
:
Dim
Dim
Dim
Dim
Dim
Dim
Dim

vMyName As Variant
iMyCounter As Integer
boPressed As Boolean
dX3 As Double, dY3 As Double, dX4 As Double, dY4 As Double
siNumber As Single
sNameOfFile As String.
lCounter As Long

(2).
. .
Variant ( ), ,
.
VB 6.0 v1.3 / e- / / -

76

( ) 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)

VB 6.0 v1.3 / e- / / -

77

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
;
; ,

VB 6.0 v1.3 / e- / / -

78


.
:
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).

VB 6.0 v1.3 / e- / / -

79

;
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.

VB 6.0 v1.3 / e- / / -

80

Visual Basic
.

.
(intrinsic functions)
VB ,

.
:
) :
Sin (), Cos
(), bs ( ) . :

Sin(a)
Cos(a)
Tan(a)
Atn(a)

Abs(a)

Int(a)

Rnd *


0

.

>0

>=0


ea

Log(a)
Sqr(a)

Sgn(a)

Exp()

>0 1
=0 0
<0 -1

VB 6.0 v1.3 / e- / / -

81


, .
Microsoft ( VB)
,
:

Atn(a / Sqr(-a * a + 1))


Atn(-a/Sqr(-a * a + 1)) + 2 * Atn(1)
Log(a) / Log(N)
a mod b
a mod b = a int(a/b) * b

(Arcsin)
(Arccos)

Modulo ( )

a = Sin(b)
a = Cos(b)
,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.

VB 6.0 v1.3 / e- / / -

82

(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)

VB 6.0 v1.3 / e- / / -

83

: ( .. 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 ( ).

.

VB 6.0 v1.3 / e- / / -

84


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

VB 6.0 v1.3 / e- / / -

85

Left(aString,
iLength)

Right(aString,
iLength)

Trim(aString)

Ltrim(aString)

Rtrim(aString)



aString

(
)
.


aString
(
)

.




.



.



.




(),
()

Variant String




(),
()

Variant String




().




().




().

VB 6.0 v1.3 / e- / / -

86

:
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). .

.

VB 6.0 v1.3 / e- / / -

87

) :
,
.

Now

Date

Time
Year(aDate)

Month(aDate)

Day(aDate)

Hour(aTime)

Minute(aTime)

Second(aTime)

.

.


.



.



.


.


.


.


:
Dim MyDate As Date
, Variant
.

VB 6.0 v1.3 / e- / / -

88

:
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.

VB 6.0 v1.3 / e- / / -

89

HelpFile :
.
Context : Visual Basic
( ).
.
:

vbInformation + vbOKCancel


. HelpFile
Context
.
Prompt
.
(Active
Interface).

VB 6.0 v1.3 / e- / / -

90

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
.

VB 6.0 v1.3 / e- / / -

91

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
Debug.Print
Debug.Print
Debug.Print

Format(dNum1,
Format(dNum1,
Format(dNum1,
Format(dNum1,

"0.000")
"0000.0000")
"-0.0")
"0.00000000000")

Debug.Print Format(dNum2, "0.00%")


Debug.Print Format(dNum2 * dNum1 / (dNum2 + dNum1), ".000%")
End Sub

VB 6.0 v1.3 / e- / / -

92

Format
.

Visual Basic:

VB 6.0 v1.3 / e- / / -

93

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", "<") ' "hello".
Debug.Print MyStr
MyStr = Format("This is it", ">") ' "THIS IS IT".
Debug.Print MyStr
End Sub

Ta :

VB 6.0 v1.3 / e- / / -

94

,
, .
ReMark, Compiler Visual Basic,

, .
.

VB 6.0 v1.3 / e- / / -

95

;
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 .

VB 6.0 v1.3 / e- / / -

96

Visual Basic
.

:

2 .
,
.
.
/.
,
.

; Visual Basic
:

, .
,
set .

IfThenElse(End If)
:
If Then Else
:
MyNum1 = 12
MyNum2 = 23
If MyNum1 > MyNum2 Then MsgBox ! Else
MsgBox O !
;
;

VB 6.0 v1.3 / e- / / -

97

If...Then...Else
.

. ,
.

=
>
<
>=
=<
<>


(
,
).

(argument): , ,
, , (), ..
Cos(3.14), 3.14

MsgBox !,

MsgBox
.
Y
.

,
10. ,
:

And
Not
Or

()

()

VB 6.0 v1.3 / e- / / -

98

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

If X1 < X2 Or Y1 = Y2 Then
Z1 = Z2
Else
Z2 = Z1
End If
Debug.Print Z1 , Z2

VB 6.0 v1.3 / e- / / -

99

; : 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
.

VB 6.0 v1.3 / e- / / -

100

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!.
!

VB 6.0 v1.3 / e- / / -

101

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
Is
,
Else

(num1 To num2)
+ / : (.. Is > 23 , Is<=12 ).
And Block If. (. 12, 23, 17, 9 )
. Else Block If.

.
.

VB 6.0 v1.3 / e- / / -

102

Private Sub Command1_Click()


Dim i As Integer
Randomize Timer
i = Int(Rnd * 100)
Debug.Print "Number i = "; i

[0,100)

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 :

VB 6.0 v1.3 / e- / / -

103

Private Sub Command2_Click()


Dim i As Integer
Randomize Timer
i = Int(Rnd * 100)
Debug.Print "Number i = "; i

[0,100)

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.

VB 6.0 v1.3 / e- / / -

104

;
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.
.
.
,
. ,
. ,
.

VB 6.0 v1.3 / e- / / -

105

7
( )

Visual Basic

.

. ,
.
:
40 .
40 . ,
.
. ,
.
Visual Basic.
,
,
.
, project
10

( ). 10
. ,
( ), 100 ...!
,
.

VB 6.0 v1.3 / e- / / -

106

For [Step] Next


.
:
For = [Step ]

Next

:
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 10
Debug.Print i * 10
Next i

1 i 10.
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.
,
:

VB 6.0 v1.3 / e- / / -

107

1
2
3
4
5
6
7
8
9
10

i
1
2
3
4
5
6
7
8
9
10
11

10
20
30
40
50
60
70
80
90
100
() 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

1
2
3
4
5

i
1
3
5
7
9
11

10
30
50
70
90
() 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

VB 6.0 v1.3 / e- / / -

108

= 2 ( Next). i 3,
ForNext .

1
2
3
4

i
10
8
6
4
2

100
80
60
40
() i 2
(<3)
)


(embedded loop):
Private Sub Command1_Click()
For i = 1 To 4
For j = 3 To 1 Step -1
Debug.Print i, j, i * j * 10
Next j
Next i
End Sub


.
) For i = 1 To 4 i 4 (
1 4), i = 1 = 1.

) For j = 3 To 1 Step 1 j
3 ( 3 1), j = 3 = -1.
) Debug.Print i, j, i * j * 10 i, j, i
* j * 10
) Next j j 1 .
j 0 (<1)

) Next i i 1 .
i 5 (>4) (
) .

VB 6.0 v1.3 / e- / / -

109

i
1

j
3
2
1
3
2
1
3
2
1
3
2
1

i , j, i * j * 10
1
1
1
2
2
2
3
3
3
4
4
4

3
2
1
3
2
1
3
2
1
3
2
1

30
20
10
60
40
20
90
60
30
120
80
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

VB 6.0 v1.3 / e- / / -

110

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) <= 20 Then
If Cords(j, 2) >= 0 And Cords(j, 2) <= 20 Then
MsgBox " ! " & j
Exit For
End If
End If
Next j

;
> 100 .
>> .
( 20)
>>> .

) j.
) , .
<<< Block .
<< Block .
< , j .

Next j ( ).

VB 6.0 v1.3 / e- / / -

111


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.

( ).
.
.

VB 6.0 v1.3 / e- / / -

112

WhileWend
.
:
While

Wend
.
:
Private Sub Command1_Click()
Dim x As Single
x = 10
While x >= 0 x 0
Debug.Print x x
x 1
x = 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, .

VB 6.0 v1.3 / e- / / -

113



. ,

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
.

VB 6.0 v1.3 / e- / / -

114

Do While / Until Loop


.
:

Do While

(DoLoop).


[Exit Do]
WhileWend.

Exit Do
Loop
Do Until


[Exit Do]

Loop

(
Exit For).

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 :

VB 6.0 v1.3 / e- / / -

115

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 !!!


:

VB 6.0 v1.3 / e- / / -

116

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.
UBound

Dim ...
MyAr(-6 22)
MyAr(-6 22, -3 8)

LBound

-6

22-(-6)=28
28
11

22
22

-6

-3

VB 6.0 v1.3 / e- / / -

117

:
-6 22, -3 8.
,

.

.

VB 6.0 v1.3 / e- / / -

118

;
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 ;

VB 6.0 v1.3 / e- / / -

119


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].

VB 6.0 v1.3 / e- / / -

120

8
()


/. .. Microsoft Word
,
, , .
.
, ,
.
() .
( )
.
2 : ( /
sequential) (direct, / random) .

VB 6.0 v1.3 / e- / / -

121

( / 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: .
, .

VB 6.0 v1.3 / e- / / -

122

, .

.
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

VB 6.0 v1.3 / e- / / -

123

:
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].
,
, .

VB 6.0 v1.3 / e- / / -

124

:
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

,
,
.

VB 6.0 v1.3 / e- / / -

125


,
.
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

. ,
, .

VB 6.0 v1.3 / e- / / -

126

WhileWend.
, Not.
While [While Not EOF(n) /. / Wend]

.
(
bytes) LOF(n).
LOF (Length Of File )
:
LOF(n)
:
LengthOfMyFile = LOF(1)
H ( Bytes)
1 LengthOfMyFile.
, ,
, .
.

VB 6.0 v1.3 / e- / / -

127

(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

VB 6.0 v1.3 / e- / / -

128

(10 10)
ForNext .

. !
Print #15, Pin(i, j),

, .

. For jNext o
For iNext j i .
,
, ,
!
Print #15, ""

.
.

, .

VB 6.0 v1.3 / e- / / -

129

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

VB 6.0 v1.3 / e- / / -

130

.
.

.
2 ForNext
.
,
. , ,
Format . :


, Common Dialog Control
.

VB 6.0 v1.3 / e- / / -

131

(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 :

VB 6.0 v1.3 / e- / / -

132

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
!

VB 6.0 v1.3 / e- / / -

133

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)
Case 1 To 2
ByCase(2) = ByCase(2)
Case 2 To 3
ByCase(3) = ByCase(3)
Case 3 To 5
ByCase(4) = ByCase(4)
End Select

+ 1
+ 1
+ 1
+ 1

Wend
Debug.Print ByCase(1), ByCase(2), ByCase(3), ByCase(4)
Close #1
End Sub

VB 6.0 v1.3 / e- / / -

134

.
Select Case. , ,
.
[0...1], (1...2], (2...3], (3...5]. 1 ; ( 2
3); ( 1 Case ).
2 ; !
Select Case, ,
Case .
Case, End Select
.

VB 6.0 v1.3 / e- / / -

135

(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
.

VB 6.0 v1.3 / e- / / -

136

_ (UnderScore). H
.
,
, _
[Enter]. H Visual Basic
. ,
.
,
,
.
:

George Newcomer .
& MsgBox
,
.

.
(Random Access Files).

VB 6.0 v1.3 / e- / / -

137

( / Direct,
/ Random access files):

,
5
4 .

. :
1.
, .
.
,
.
2. ,
,
,
. .
3.
, /
.
4.
Text Editor. .

.
5.
.
Format
.
,
Format .
.
;
Visual Basic,
Type. ,
Type Module
(
) .

VB 6.0 v1.3 / e- / / -

138

Visual Basic
Module :
Menu
, () [Project]
[Add Module].

Menu [Project]
.
Open. Module Module1
project , () .

Module;
Module .
( ). .
Module
( project ),
.
(
projects),
. ,
. ,
-
.

VB 6.0 v1.3 / e- / / -

139

, 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)

!

VB 6.0 v1.3 / e- / / -

140

.
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

VB 6.0 v1.3 / e- / / -

141

: 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

VB 6.0 v1.3 / e- / / -

142

, 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.
,
,
.
( ).

VB 6.0 v1.3 / e- / / -

143

;
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
;

VB 6.0 v1.3 / e- / / -

144


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. .
,
.

VB 6.0 v1.3 / e- / / -

145

9
Modules, (sub-routines), (functions)

! ,
, ,
, ,
, (,
, ) . -

.
:
With CD1
.DialogTitle = " "
.Filter = " *.txt (*.txt)|*.txt| *.dat (*.dat)|*.dat"
.FileName = "Myfile.txt"
.ShowOpen
End With
MyFile = CD1.FileName

,
; !
;
, .
, .


. ,

.

VB 6.0 v1.3 / e- / / -

146

(Sub-Routines)


.

Module.
,

.
,

Module.
.
,
Module. ;

Module, project.
Menu [Project] >> [Add Module] [Existing]

Module
project project .
VB 6.0 v1.3 / e- / / -

147


, .


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,


.

VB 6.0 v1.3 / e- / / -

148

Module ,
.

:

Private Sub Command6_Click()


Dim NewPnt As Cords, i As Integer

Private Sub Command6_Click()


Dim NewPnt As Cords, i As Integer

With CD1
.DialogTitle = "
"
.Filter = *.* (*.*)|*.*|
*.dat (*.dat)|*.dat"
.FileName = "Myfile.dat"
.ShowOpen
End With

Module1.OpFile

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

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

Close #1
End Sub

,

Module1.OpFile .
(
) Module
,
.
OpFile
Module1,
VB 6.0 v1.3 / e- / / -

149

MyFile, Module1 (
Common Dialog
Module1).
O .
,
.
2 .
Module1.
Public Sum As Double
Public Sub DoSum(num1 As Double, num2 As Double)
Sum = num1 + num2
End Sub

Module1.
,
.
Command9 :
Private Sub Command9_Click()
Module1.DoSum 23.346, 66.55
Debug.Print Sum
End Sub

,

2 ( ).


.
( ).

,
.

VB 6.0 v1.3 / e- / / -

150

(Function)
.

y = F(x) = x2 6*x +23


x
y.
. (
, Module) , .
: Function
,
( ).
,
.

, .
3 :
( Module1)
Public Function Multiply(num1 As Double, num2 As Double, num3 As Integer) _
As Double
Multiply = num1 * num2 * num3
End Function


.
Command10.
Private Sub Command10_Click()
Dim MyNum As Double
MyNum = Multiply(23.4545, 12.66, 3)
Debug.Print MyNum
End Sub


) MyNum
Double,
Double.

VB 6.0 v1.3 / e- / / -

151

) (
) Module .
:
MyNum = Multiply(23.4545, 12.66, 3)


MyNum = Module1.Multiply(23.4545, 12.66, 3)

.

:
Dim vResult As Double
VResult = 45 * Log(Multiply(12.11, 3.6543, 7))

()
:
Public Type Cords2D
X As Double
Y As Double
End Type
Public Function AThem(BasePoint As Cords2D, Distance As Double, _
G_Rad As Double) As Cords2D
AThem.X = BasePoint.X + Distance * Sin(G_Rad)
AThem.Y = BasePoint.Y + Distance * Cos(G_Rad)
End Function

H
, ,
.
( ).

Command11, :

VB 6.0 v1.3 / e- / / -

152

Private Sub Command11_Click()


Dim BaseP As Cords2D, PntFound As Cords2D
Dim Dis As Double, Gonia As Double
BaseP.X = 234.777
BaseP.Y = -129.016
Dis = 36.813
Gonia = 0.23455343
PntFound = AThem(BaseP, Dis, Gonia)
Debug.Print PntFound.X, PntFound.Y
End Sub


. .

Module,
modTopoFunctions .
Module
,
.
Module :
1.
.
2. () .
3.
.

VB 6.0 v1.3 / e- / / -

153

;
1. Modules ,
, .
2.
( , Integer, Double ).
3.
.
4.
.
, .
5. Modules ,
.
Module.

1.
, .
( )
;
2. a Module;
3. Module,
;
4.
;

1. project, Module

, 2
.
2. project, Module
.
.
3. project, Module
N
.
4. project, Module

(/),
5 .
.

VB 6.0 v1.3 / e- / / -

154


VISUAL BASIC for APPLICATIONS

Visual Basic for Application (VBA)


Microsoft Visual Basic. Basic

.
Light
, ,
. Ms VB

. VBA .
MsOffice, StarOffice, AutoCad,
MicroStation .
( ) ,
, ,
.
.
VBA Ms VB
.
.
, Excel
.

Excel.

VB 6.0 v1.3 / e- / / -

155

1. VBA MsExcel
VBA Excel
Alt+F11 Menu [Tools]  [Macro]  [Visual Basic
Editor]
Project
Explorer
, Properties Window
.
project .
Immediate Window
Ctrl+G menu [View]  [Immediate Window].
: menu [Insert]  [UserForm]
UserForm1
ToolBox. ; VBA
3 . project interface
(buttons, textboxes ). module.
project. interface
.
Class module
.
: Userform1
form1.
(: name caption),
Userform1.
:
Private Sub UserForm_Click()
End Sub

Click.
Visual Basic.
(project explorer  View
object).

156

, ,
, (option button).

.

.

.

Microsoft Visual Basic

Text1
Label1
Command1
Check1
Option1

Visual Basic for


Applications (VBA)
TextBox1
Label1
CommandButton1
CheckBox1
OptionButton1

157

2. VBA MsExcel & AutoCad


1. ,

.
Private Sub CommandButton1_Click()
' :
' q1, q2 .
q1 = InputBox("", " ", "A")
q2 = InputBox("", " ", "1")
' string .. "1"
CelName = q1 + q2
' CelV
CelV = Worksheets("Sheet1").Range(CelName).Value

' "Blank cell"


If CelV = "" Then CelV = "Blank cell"
' Textbox1
TextBox1.Text = CelV
End Sub

Private Sub CommandButton2_Click()


' :
' q1, q2

q1 = InputBox("", " ", "C")
q2 = InputBox("", " ", "1")
'
Mul = InputBox("", " ", "2")
' string .. "C1"
CelName = q1 + q2
' CelV * Mul
Worksheets("Sheet1").Range(CelName).Value = Val(TextBox1.Text) * Mul
End Sub

158

2. .
(bold &
red)

Private Sub CommandButton1_Click()


'c
' 1 D10
For Each c In Worksheets("Sheet1").Range("A1:D10")
' ...
If c.Value <> Val(c.Value) Then
'
c.Font.Bold = True
'
c.Font.Color = vbRed
End If
Next c
End Sub

159

3. seq
Excel. O :
, , ,
Private Sub CommandButton1_Click()
' :
' .
Textbox1
NamOfFile = TextBox1.Text
' A,B,C,D

Worksheets("Sheet1").Range("A1").Value = ""
Worksheets("Sheet1").Range("B1").Value = "X (m)"
Worksheets("Sheet1").Range("C1").Value = "Y (m)"
Worksheets("Sheet1").Range("D1").Value = "H (m)"
' Excel
MyLine = 1
' 1 buffer
Open NamOfFile For Input As #1
' : Loop
While Not EOF(1)
' ,,,
Input #1, COD, XX, YY, HH
' .
MyLine = MyLine + 1
' .

,
.
POS1 = "A" & MyLine
POS2 = "B" & MyLineCreated by Information Technology Center ()
POS3 = "C" & MyLine
POS4 = "D" & MyLine
' . , C D

Worksheets("Sheet1").Range(POS1).Value = COD
Worksheets("Sheet1").Range(POS2).Value = XX
Worksheets("Sheet1").Range(POS3).Value = YY
Worksheets("Sheet1").Range(POS4).Value = HH

160

' Loop -
Wend
' ( )
Close #1
End Sub

161

4.
AutoCad.

.
Private Sub CommandButton1_Click()
'
Dim pcords(0 To 2) As Double
Dim tcords(0 To 2) As Double
' 2
Dim apo As AcadPoint
Dim ate As AcadText
' AutoCad
Dim activeDoc As AcadDocument
Dim mspace As AcadModelSpace
'
' : ___._.__
'___.__
Set activeDoc = ThisDrawing.Application.ActiveDocument
Set mspace = ThisDrawing.ModelSpace
' 500
For i = 1 To 500
Randomize Timer
' 0
pcords(0) = Rnd * 1000 + 1000
Randomize Timer
' 1
pcords(1) = Rnd * 1000 + 1000
Randomize Timer
' 2
pcords(2) = Rnd * 100 + 100
' ""
'
tcords(0) = pcords(0) + 0.2
tcords(1) = pcords(1) + 0.2
' 0.000
tcords(2) = 0
' pcords
Set apo = mspace.AddPoint(pcords)

162

' (. = i) tcords = 0.9


Set ate = mspace.AddText(i, tcords, 0.9)
'Zoom
ZoomAll
'
Next i
End Sub

163

1 : (n x m) .

Private Sub Form_Click()


' "" .
': .
' x,y,z
Dim x As Double, y As Double, z As Double
' ar 3 x 3 ,

Dim ar(3, 3) As Double
' "inputfile1.txt" "" 1
': ,
Open "inputfile1.txt" For Input As #1
' "outfile1.txt" "" 2
': .

Open "outfile1.txt" For Output As #2


' 1,
': .
"," .
Input #1, x, y, z
' .
': Form1 "" . "Form1"
.
' (, ..) Visual Basic.
, .
' Form1 Print x,y,z
.
Form1.Print x, y, z

164

' x,y,z ar, 1


1,2,3
ar(1, 1) = x
ar(1, 2) = y
ar(1, 3) = z
' Form1 ( )
Form1.Print ar(1, 1), ar(1, 2), ar(1, 3)
' 2,
(1,1) (1,2) (1,3) ar
Print #2, ar(1, 1), ar(1, 2), ar(1, 3)
' .
Form1.Print " "
' 1,2
Close #1
Close #2
' ---> :
End Sub

1 :
1. Form1 .
.
, Print , Move
, Cls ..

. Click ,
DblClick ( ) , Resize ( ) , Unload ()
..
.
Caption () , Font ( ) ,
ForeColor ( ) , BackColor ( ) , Left
( ) , Top ( ) , Width
() , Height() ..
2.

Me .
Me.Print Form1.Print.
.

165

3.
1 .

FreeFile ,
.. ff :
ff = FreeFile
Open aaa.txt For Input As ff

FreeFile.
[1 255]

166

2 : (Sqr) , (Sin),
(Cos), (Tan).
45.
(=3,14159)

Private Sub Form_Click()


' "" .
': .
' p, sin45, cos45, tan45
Dim p As Double
Dim sin45 As Double, cos45 As Double , tan45 As Double
'
p = 4# * Atn(1#)
' "Greek pi" .
Form2.Print "Greek pi"
'
Form2.Print p
' "( 2) / 2 " .
Form2.Print "( 2) / 2 "
'
Form2.Print Sqr(2) / 2
' "sin cos tan of 45 degrees" .
Form2.Print "sin, cos, tan of 45 degrees"
' , 45
': rad ().
,
' . ""
180.
sin45 = Sin(45 * p / 180)
cos45 = Cos(45 * p / 180)
tan45 = Tan(45 * p / 180)
' .
,
' Tab
Form2.Print sin45, cos45, tan45
' ---> :
End Sub

167

2 :
1. H Visual Basic
.
.
Double,
.
.
2.
.
.
( ).
. /
, \
. 8/3 = 2.6666667 8\3 = 2 .
3. .
.

168

3 : Label TextBox,
ForNext

Private Sub Form_Click()


' "" .
': .
'
(Label)
' (TextBox).
' Label Label1 TextBox
'Text1 Text2
' p
Dim p As Double
' Caption (= ) Label1

"greek p"
label1 = "greek p"
' ""
p = 4 * Atn(1)
' Text (= ) Text1
Text1 = p
' 10
For i = 1 To 10
' Text2 "i"
1 10
Form3.Print i
Text2 = i
'
Next i
' ---> :
End Sub

169

3 :
1. Label Textbox
, .
. Label
Caption, .
:
Label1 = My name is Q
Label1.Caption = My name is Q

.
TextBox. TextBox
Text.
Text1 = Hi!
Text1.Text = Hi!
.
.
2. ForNext [Step]
.
:
For = ( ) To ( ) [Step ]

Next
:
For i = 10 To 100 Step 10

Next i
Step .
i.

170

i 10, 20, 30, , 100


.
,
(..
) Exit For.

171

4 : Do/While, For/Next, If/Then/Else, InputBox


TextBox.

Private Sub Command1_Click()


' "" Command1
': .
'
Dim a(1, 3) As Double
' Long ( ): Long Integer

Dim repetitions As Long, initial_value As Long
'
Dim reply As Integer
' initial_value
initial_value = 0
' ,
': '
!!
'
.
'
initial_value < 10000
Do While (initial_value < 10000)
' For/Next
For i = 1 To 3
' - : (=1) .
1
'initial_value +1 , 2 initial_value +2, 3 initial_value +3
a(1, i) = initial_value + i
'
Next i

' TextBox1, TextBox2, TextBox3


( Text)
Text1 = a(1, 1)
Text2 = a(1, 2)
Text3 = a(1, 3)
' InputBox

172

' .
' :
'Inputbox( , , )
' (
) : "
; (1- 0-)"
' Windows
InputBox
'H ( "1")

reply = InputBox(" ; (1- 0-)","","1")
'
If (reply = 1) Then
'

' initial_value 10
initial_value = initial_value + 10
'
Else
'
Exit Sub
'
End If

' Do While / Loop


Loop
' ---> :
End Sub

Private Sub Command2_Click()


' "" Command2
' - Visual Basic
End
'
End Sub

173

4 :
1. Do While / Loop While / Wend

.
If..Then..Else..End If Exit Sub
:
initial_value = 10000
10000 initial_value
While
.
2. Do While / Loop
:
While initial_value < 10000

Wend

.
3. If/Then//Else/End If If Block.
. :
If Then

Else

End If

174

If val1 <= 30 then

If Answer = True then


...

4. InputBox ,
Input Basic
.

.

175

5 : . 1
.

Private Sub Form_Load()


' .
': LOAD

'
Dim x0 As Double, y0 As Double, s As Double, g As Double
Dim dx As Double, dy As Double
' "inputfile2.txt" "" 1
': ,
Open "inputfile2.txt" For Input As #1
' "outfile2.txt" "" 2
': .

Open "outfile2.txt" For Output As #2
' = 3,14159...
pi = 4 * Atn(1)
' ( 1) , x0 y0
Input #1, x0, y0
' Do While / Loop ( While/Wend )
' EOF END OF FILE.
'
.
' " 1
".
' "" .
Do While Not EOF(1)
' .
s,g
Input #1, s, g
' dx,dy.
': x0,y0,s,g,pi .
"=" (
" "). x0,y0.
= 0.
dx = x0 + 13.434

176

dx = x0 + s * Sin(g * pi / 200)
dy = y0 + s * Cos(g * pi / 200)
' Text1 Text2 ( Text) dx,dy
Text1.Text = dx
Text2.Text = dy
' dx,dy ( 2)
Print #2, dx, dy
'
Loop

'
Close #1
Close #2
' . .
"" "" .
End Sub

5 :
1. ascii
Editor (.. Notepad).
5

.
.
2. EOF
.

. LOF (Length Of File)
bytes.
LOF()= 0 . LOC


bytes. 128.
3. : ..

177

Pl1 = K2
Text1.Text = 57.983
K3 = Text2.Text
=
. :
Mul1 = Mul1 * 2
Sum = Sum + Cop1
.

.

178

6 : (Random Access File).


Type ( )

'---------------------------------------------------'---------------------------------------------------' .
' General [Declarations]
' Code. ( )
' . "xyz"
3 : x,y,z
'T
Private Type xyz
x As Double
y As Double
z As Double
End Type
'

' xyz1 xyz


': xyz .
xyz1 xyz
Private xyz1 As xyz
'---------------------------------------------------'----------------------------------------------------

Private Sub Command1_Click()


' "" Command1
' irec
Dim irec As Integer
' length xyz1
Len.
'H Len
Bytes.
' xyz1 3 , x,y,z

' , "" (= ) xyz1
3 * 8 = 24 bytes ( 8 bytes
' double.
Length = Len(xyz1)

' "randomfile7"

179

1 ( ) 24 (bytes)
Open "randomfile7" For Random As #1 Len = length
' ( = 100)
For k = 1 To 100
'
xyz1
xyz1.x =
xyz1.y =
xyz1.z =

k+1, k+2 , k+3 x,y,z


k + 1
k + 2
k + 3

' . k For/Next

' ""

Put #1, k, xyz1
'
Next k
'
Close #1
'
11
Open "randomfile7" For Random As #11 Len = length
' InputBox
.
' "irec" "k"
.
'
.
':
irec = InputBox("Record number ")
' xyz1
irec
Get #11, irec, xyz1
' Text1 , Text2 , Text3 (
) Text
(.. Text1.Text = xyz1.x)
.
Text1 = xyz1.x
Text2 = xyz1.y
Text3 = xyz1.z
'

180

Close #11
' -
End Sub

6 :
1.
. :
a. editor
.
b.
.
c.
.
d.
.
:
Open For Random As Len =
..:
Open DATA1.dat For Random As #3 Len = 36

.
e. Input Print
Get Put .

. Put
Get :
Put , ,
Get , ,
..:
Put #12, iKey , X , Y

181

Get #12, iKey , X , Y


( iKey)
.

2. Type/End Type
.
. ,
.
:
Private Type New_Line
Xstart As Double
Ystart As Double
Zstart As Double
Xend As Double
Yend As Double
Zend As Double
Line_Type As Variant
Visibility As Boolean
Line_Drawn As Boolean
End Type

- .
.
.
,
.
:
Dim MyLine(3000) As New_Line

3000 New_Line
1 :

182

MyLine
New_Line .
,

. :
MyLine(1).LineDrawn = False
MyLine(1).Xstart = 500.45
MyLine(1).Ystart = 300.16
MyLine(1).Zstart = 800.99
3
1
.
.
Boolean
,
True False. ,
.. Pos1 > 0 MyLine(1).Visibility = True
3. Len:
( bytes).
Len(Fanis) 5 (5 )

183


1. : 2 x, y
.
a. .
G() S(A<>B)
b.
: = 3,14159 ,( ) (: )
c. :
i.
ii. ( InputBox)
iii.
.
iv.
.
v. .

i)
:
Dim Xa As Double, Ya As Double, Xb As Double
Dim Yb As Double, Sab As Double, Gab As Double
Dim Pi As Double
Pi = 4 * Atn(1)
ii)
:
Open MyFile.txt For Input As #1
Input #1, Xa, Ya, Xb, Yb
Close #1
iii)
:
Dx = Xb Xa
Dy = Yb - Ya

184

If Dx = 0 And Dy = 0 Then Exit Sub T


Sab = Sqr((Dx)^2 + (Dy)^2)
If Dy<> 0 Then
Gab = Atn(Dx/Dy)
End If
Gab = Abs(Gab * 200 / pi)

iv)

:
If Dx > 0 And Dy > 0 Then G = G
If Dx > 0 And Dy < 0 Then G = 200 - G
If Dx < 0 And Dy < 0 Then G = 200 + G
If Dx < 0 And Dy > 0 Then G = 400 - G
Dx / Dy = 0
If Dy > 0 And Dx = 0 Then
G=0
End If
If Dy < 0 And Dx = 0 Then
G = 200
End If
If Dx > 0 And Dy = 0 Then
G = 100
End If
If Dx < 0 And Dy = 0 Then
G = 300
End If
v)
:
Msgbox H S(,) & Format(Sab,0.000) & (m) &
VbCrLf & VbCrLf & G(A,B) &
Format(Gab,0.0000) & (grad) , vbInformation, " 2
"

185

MsgBox.
,
.
Format:
Format ( , Format )
..
Met1 = 345.212345
Met2 = Format ( Met1 , 0.00)
H Met2 345.21

VbCrLf
H VbCrLf (2 )
.
:
Chr(13) + Chr(10)
:
NeLi = Chr(13) + Chr(10)
MsgBox
:
Msgbox H S(,) & Format(Sab,0.000) & (m) & NeLi
& NeLi & G(A,B) & Format(Gab,0.0000) &
(grad) , vbInformation, " 2 "

186

2.
a.
b. /
c.
(a)
:
1)
T = (a + b + c) / 2

= Sqr( T * (T-a) * (T-b) * (T-c) )
2)
= 0.5 * b * c * Sin(A)
3)
= 0.5 * base * height
base , height
.
(b)
:
= pi * R^2
R :

pi = 3,14159  pi = 4 * Atn(1)

:
= pi * R^2 * m / 400
m :

(c)
:
For I = 1 To Vertices 1

Vertices
() ,()
adxi = X(I) + X(I + 1)
adxi:
adyi = Y(I) - Y(I + 1)
adyi: Y
Emb_plus = 0.5 * (adxi * adyi)
E = E + Emb_plus

Next I
= Abs (E)

187

DLL

DLL (Dynamic Link Library /


)
/ , .
DLL
.
Module; ,
.
1. Module ,
,
Module ( )
.

. , DLL
Project , ,
,
.
2.
, Module
. Module
.
, DLL compiled ,

.
3. DLLs
, . ,
DLL Visual Basic,
C, C++, Delphi, Fortran
.
. DLL
. ,

188

,
DLL .
DLL.
Visual Basic project
Standard EXE , ActiveX DLL.

Class Module ( ),
Module,
.
Class Module Class1.
, ,
.
1: (Class Module).
.
clMat ( Name).

189

2: 2 .
Module.
Public Function Rand(FromNumber As Double, ToNumber As Double, IntegerType _
As Boolean) As Double
Dim TheSpace As Double
TheSpace = ToNumber - FromNumber
If TheSpace <= 0 Then
MsgBox " !", vbCritical, "!"
Exit Function
Else
Randomize Timer
If IntegerType = True Then
Rand = Int((Rnd * TheSpace) + FromNumber)
Else
Rand = Rnd * TheSpace + FromNumber
End If
End If
End Function

,
.
,
IntegerType ,
.
:
Public Function Add2(num1 As Double, num2 As Double) As Double
Add2 = num1 + num2
End Function

Add2
.
.
3: Menu Visual Basic
[Project] [Project1 Properties].
,
( MathFunc). .

190

Menu > [Project] [Project1


Properties] [MathFunc Properties].
.
4: .
Menu, [File]
[Save Project As]. H
Common Dialog Visual Basic (
).
.
( )
Save ( :
project Class Module).
, DLL.
5: DLL: Menu > [File]
[Make MathFunc.dll]. ( ,
project
).
.
.
DLL

191

.
(DLL). Visual Basic.


.
1: Visual Basic,
Standard EXE project. .
2: Menu [Project]
[References]. ,
Browse
MathFunc.dll.

, Open. To DLL
, ().
project .
!

3: ,
:

192

Private Sub Command1_Click()


Dim MyRand As clMat, Anum As Double, Bnum As Double
Set MyRand = New clMat
Anum = MyRand.Rand(7, 23, True)
MsgBox Anum, vbInformation, "Integer"
Bnum = MyRand.Rand(5, 59, False)
MsgBox Bnum, vbInformation, "Real"
End Sub

.
, MyRand clMat o

.
.

Set MyRand = New clMat.

Visual Basic MyRand


( clMat)
clMat
.
.
Rand
DLL , MyRand
clMat.
, VB

,
.

, :

193

Command1,
, .
MsgBox:

Command2:
Private Sub Command2_Click()
Dim MyAdded As clMat, Sum As Double
Set MyAdded = New clMat
Sum = MyAdded.Add2(12.455, 34.6565)
MsgBox " " & Sum, vbInformation, " Add2"
End Sub


Command1. 2 ,

, Sum. :

Dll , ,
. , DLL
, o
Windows. :
System32 Windows, Regsvr32.exe
(Regsvr32.exe MyDll.DLL).

194

DLL
project.

1. DLL
3 .
2. DLL ,
2 .
3. DLL
.

195


, ..
.
,
( )
.

End:
End
.
[X]
(ControlBox).

,
.
: QueryUnload. (
). :
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox(" ;", _
vbCritical + vbYesNo, " ") = vbNo Then
Cancel = 1
End If
End Sub

[X] :

196

Yes . No
.

Cancel (Boolean) End.
.
, MsgBox ,
IfEnd If,
.
End
:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox(" ;", _
vbCritical + vbYesNo, " ") = vbYes Then
End
Else
Cancel = 1
End If
End Sub

End Menu
Cancel .

DoEvents:
,
. , Visual Basic
( )
Interface .
.
,
.
DoEvents .
Interface .

. DoEvents.
: (
Module)
Public iPress As Integer

Module

197

Private Sub Command1_Click()


iPress = 0
While iPress <> 1
i = i + 1
Debug.Print i
Wend
End Sub
Private Sub Command2_Click()
iPress = 1
End Sub

(Command1).
Immediate Window i (
WhileWend.
iPress 1. Command2.
! ; Command2
!
[Ctrl] + [Scroll Lock]
{Stop} Visual Basic

Command1 ( DoEvents)
:
Private Sub Command1_Click()
iPress = 0
While iPress <> 1
DoEvents
i = i + 1
Debug.Print i
Wend
End Sub

198

Command1.
Command2. ; Command2
!
DoEvents ,
.

Stop:
H Stop
,
. .
[Ctrl] + [Scroll Lock]
.
Stop
[F5] [Continue] Menu [Run] Visual Basic.
,

.
:

.
i
.

Enum:
(enumerate) TypeEnd Type
. : Enum
.
Module. :

199

Public Enum MyColors


Red = vbRed
Green = vbGreen
Yellow = vbYellow
Black = vbBlack
White = vbWhite
Cyan = vbCyan
Blue = vbBlue
Magenta = vbMagenta
End Enum
Private Sub Command1_Click()
Form1.BackColor = MyColors.Yellow
End Sub

Enum , MyColors
Red, Green Visual Basic
( , vb).

MyColors.Yellow:


Enum
. .

Def:
.
( ... ).
,
.
Module.

200

DefBool
( Boolean)
DefByte
( Byte)
DefInt
( Integer)
DefLng
( Long)
DefSng
( Single)
DefDbl
( Double)
DefDate
( Date)
DefStr
( String)
DefVar
( Variant)

DefBool LetterRange
[,LetterRange]
DefByte LetterRange
[,LetterRange]
DefInt LetterRange
[,LetterRange]
DefLng LetterRange
[,LetterRange]
DefSng LetterRange
[,LetterRange]
DefDbl LetterRange
[,LetterRange]
DefDate LetterRange
[,LetterRange]
DefStr LetterRange
[,LetterRange]
DefVar LetterRange
[,LetterRange]

DefBool B-C
DefByte A, Y - Z
DefInt I K
DefLng L N
DefSng S - U
DefDbl D - H
DefDate W
DefStr O - R
DefVar V




(


1 2),



.
:
DefInt I K

i, j, k

.

Dim,
bAsked True/False
b, b Boolean ().

(error trapping):
H (bugs)
.
.
,
.
,
. , ,
( )

! ( Murphy!).

201


, Testers
, .

:
1. .

, .
2.
.
3. .
, ,
.
:
, .
4. o .
, .
,
(..
).
,

.
: MsgBox
, .
.
!

. TextBox.
Private Sub Command1_Click()
Dim anum As Double, bnum As Variant
bnum = Text1.Text
On Error GoTo ErrorTrap
anum = bnum * 1
MsgBox " ", vbInformation, ""
Exit Sub
ErrorTrap:
Err.Clear
MsgBox " ", vbExclamation, " "

202

Text1.SetFocus
End Sub

.
:
On Error GoTo ErrorTrap:
ErrorTrap. set
, Basic
(GwBasic ..).
anum = bnum * 1 : ;
,
TextBox 1.
bnum, .
, !
! On Error
GoTo (
:).
Err.Clear ,
, VB
TextBox.

,
Exit Sub
End Sub. H
.
Err.Clear,
,
:
Err.Description :
: Debug.Print Err.Description
:
Err.Number :
: Debug.Print Err.Number
: ,
Microsoft.
Err.Source :
: Debug.Print Err.Source

203

:
.
Err.Raise :
: Err.Raise Err.Number
: Visual Basic.
.

.
.
; ,
FreeFile.
;
,
. , .

:
project Common
Dialog .
,
, .
. :

You might also like