Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Visual Basic

1 - Visual Basic -
Visual Basic (event driven language)
Basic
.
( ).
(objects), ,
, .., ,
.
Visual Basic :
(form).
(objects).
(events) .
Visual Basic (), '

.
(property) , ,
, .. (method) , . ,
' .
. , .
:
Object.Property = Setting
:
frmMyForm.BackColor = QBColor(BLUE)

frmMyForm.BackColor = OldColor

2 - Visual Basic
Visual Basic (Project) (Properties).

. ,
.

3 - (Controls) Visual Basic


() Visual Basic
, . , .
() . Visual Basic
, Form1, Command2 , .
Visual Basic, .

(Form)
(Command Button)
(Label)
(Text Box)


(Menu)
(Option Button)
(Check Box)

frm
cmd, btn
lbl
txt
hsb
vsb
mnu
opt
chk

frmFirst
cmdExit, btnCloseWindow
lblOnomata
txtMessage01
hsbRate
vsbTime
mnuMainMenu
optCountry
chkLanguage

(List)
(Combo Box)

lst
cbo

lstColors
cboYpalliloi

(caption) (name)
.
,
.
.

4 - Visual Basic
,
, .
:

Name
Caption
Name
(Command Button)
Caption
Name
(Command Button)
Caption
(Form)

frmFirstProgram
Visual Basic
cmdChangeColor

cmdDrawCircle

(, click) ,
. Visual Basic
, . Private Sub End Sub
cmdChangeColor :
Private

Sub
Static
TheColor
'

TheColor
=
TheColor
+
1
'

TheColor = IIf(TheColor<15, TheColor, 1) ' > 15

frmFirstProgram.BackColor

QBColor(TheColor)

'

End Sub
, (') (comment) Visual Basic .
cmdDrawCircle :
Private
frmFirstProgram.Circle(1000, 1000), Rnd * 1500
End Sub

Sub
' -

(, method) Circle Visual Basic ,


.
, Rnd
1 1500.

5 - Visual Basic
Visual Basic 255 , ,
(reserved words) Visual Basic,
Sub Function. , Visual Basic
.
Visual Basic, ,
.


Byte
Boolean
Integer
Long Integer
Single

%
&
!

1 Byte
1 Byte
2 Bytes
4 Bytes
4 Bytes


0 - 255
(True or Flase)
-32.768 +32.767
-2,14 . +2,14 .

Double
Currency
Date
Object
String
Variant

#
@

8 Bytes
8 Bytes
8 Bytes
4 Bytes



1/1/100 31/12/9999
65.000

(Date) #, #December 15,


2003# #17 Aug 02#.
Dim Static, Visual Basic
Varian, , , (strings),
Empty Null. Empty 0 "
", Null .
Visual Basic , , .
, :
NumberOfEmployees% = 40

' Integer

( , Variant)
Dim :
Dim
Dim
Dim
Dim Onoma As String*20

NumberOfTimes
Misthos
Eponymo

As
As
As

Integer
Double
String

(arrays) :
Dim
A(10)
As
Integer
'

10

Dim
A(20,
20)
As
Integer
'

20

20

Dim A(0 To 10, -1 To 9) As Integer


' 0 10 -1 9, . 11 11
Visual Basic :

( ).
.
.
.
Dim (local)
, . .
Static
().

. , General
Object General Declarations. ,
Module Insert.
,
. , Global :
Global
Global
Global Temperature As Single

DirectorName
EuroRate

As
As

String
Double

Visual Basic , Type Pascal.


.
, Type End Type, :
Type
ID
Name
Salary

As
As
As

EmpData
Long
String*30
Currency

End Type
(.), , :
Dim

Employee

As

EmpData

Employee.ID
Employee.Name
Employee.Salary = 1500.00

=
=

1234
Papadopoulos"

"Jonh

6 - MsgBox() MsgBox
MsgBox() InputBox() MsgBox Visual Basic
/ .
MsgBox() MsgBox '
. MsgBox()
, MsgBox ' .
:
ReturnedValue%
=
MsgBox(,
,
MsgBox , , , ,

string , string
.
. .
MsgBox() :

0
1
2
3
4
5
16


Cancel
Abort, Retry, Ignore
Yes, No, Cancel
Yes, No
Retry, Cancel
Critical


vbOKOnly
vbOKCancel
vbAbortRetryIgnore
vbYesNoCancel
vbYesNo
vbRetryCancel
vbCritical

32
48
64
0
256
512
0
4096

?
!
i
Default 1
Default 2
Default 3

vbQuestion
vbExclamation
vbInformation
vbDefaultButton1
vbDefaultButton2
vbDefaultButton3
vbApplicationModal
vbSystemModal

4 , (0-5)
, (16-64) , (default)
.
4
. , 17 (1+16+0+0) Cancel Critical. Default
. 0, . ,
Default. , .
MsgBox() , ,
:

1
2
3
4
5
6
7


Cancel
Abort
Retry
Ignore
Yes
No


vbOK
vbCancel
vbAbort
vbRetry
vbIgnore
vbYes
vbNo

,
.

7 - InputBox()
InputBox()
. Variant String,
:
ReturnedValue = InputBox(, , , xpos, ypos, , ) ' Variant
ReturnedValue$ = InputBox(, , , xpos, ypos, , ) ' String
' , .
.
. String
. xpos ypos
.
, .
enter, InputBox()
, Cancel,
String (" ").
MsgBox() InputBox() MsgBox :
Dim
sName
If
End
...
Dim
Dim
iRetVal

sName
=
InputBox$("
sName
MsgBox
"

sMsg
iRetVal
MsgBox("

As

",

"

String
InputBox()")
Then
vbCritical,
If

""

!",

As
As
"

&

sName,

35,

"

String
Integer
sName")

If

iRetVal
sMsg

ElseIf

iRetVal
sMsg

ElseIf

"

=
=

iRetVal
sMsg

7
"

=
=

2
"

End
MsgBox sMsg, 64, " MsgBox"

Then
Yes"
Then
No"
Then
Cancel"
If

8 - If ... End If
:
If bathmos < 10 Then
Print Message1
Else
Print Message2
EndIf
If (Immediate If), IIF(),
: ,
.
:
a = IIF(a>=10, 0, a)
a a<10, a>=10, 0.

9 - Select Case ... End Select


,
. Case Else .
:

Select Case iAge


Case 5
' 5
sCategory = " "
Case 13 To 19
' 13 19
sCategory = ""
Case 20 To 35, 50, 60 To 65 '
sCategory = ""
Case Is > 65
' 65
sCategory = ""
Case Else
',
sCategory = " "
End Select

10 -
Visual Basic : While ... Wend, Do ... Loop For ... Next.
While ... Wend :
While <>
... ...
Wend
<> (true), Wend
While <>. <> ,
Wend.
Do ... Loop Repeat ... Until Pascal ' ,
.
, . Until
.
:
Do Until InputFile.EOF

'

Print InputFile("Name")
InputFile.MoveNext
Loop

'
'

Until Do
. .
Do ... Loop :
Do
If InputFile("ID") = "999" Then
Exit Do
Else
Print InputFile("Name")
EndIF
InputFile.MoveNext
'
Loop Until InputFile.EOF
'

. Exit Do (
).
For ... Next ,
. :
For i = 1 To 10
' Step=1
Print InputFile("Name")
'
InputFile.MoveNext
'
Next
For i = 1 To 100 Step 5
Print i & "%"
Next
Exit For For ... Next, . Exit Do
.

11 - (Procedures)
(Procedure) '
. Sub
End Sub.
,
.
:
Sub Multiply (iFirstNumber As Integer, iSecondNumber As Integer)
iProduct = iFirstNumber * iSecondNumber
End Sub

'
'

:
Multiply iUno, iDue

12 - (Functions)
(Function) '
. Function
End Function .
,
().
:
Function Multiply (iFirstNumber As Integer, iSecondNumber As Integer) As Integer
'
Multiply = iFirstNumber * iSecondNumber
'
End Function
:
iProduct = Multiply(iUno, iDue)

Visual Basic :

Len()
Mid()
Chr()
Asc()
UCase()
LCase()
InStr()

(string)
(string)
ascii
ascii


iLen = Len("Florina")
Mid("Florina", 1, 3)
sChar = Chr(65)
iCode = Asc("A")
sUp = UCase("Florina")
sLow = LCase("Florina")
sSub = InStr("Florina", "r')

7
"Flo"
"A"
65
"FLORINA"
"florina"
4

Visual Basic .
ByVal ,
ByRef
.

13 - (Labels)
(Label) , .
, ., ,
(event).
' , ' .
:

Name ()
Caption ()

lblPassword

1 =
0 =
0 =
Alignment ()
2 =
0 =
BorderStyle ()
1 =
Font ()
UB Times, Bold, 13
Appearance ()

14 - (TextBoxes)

' . ' Text.
:
Dim sOldText, sNewText As String
sNewText = ""
sOldText = txtMyTextBox.Text
txtMyTextBox.Text = sNewText
Text default
, .
sOldText = txtMyTextBox
txtMyTextBox = sNewText
:

BorderStyle ()
Appearance ()

1 =
0 =
0 =

1 =
Font ()
UB Times, Bold, 13
PasswordChar () * #
* #
SelStart ( )

SelLength ( )

Tag ()

SelStart SelLength ()
. Tag ,
.
Multiline, True
ScrollBars, Horizontal, Vertical, Both None,
, , .

15 - (Command Buttons)
().
Enter. () '
, Caption &,
Alt
& .
Default True, Enter,
Cancel True, Esc.
:

Appearance ()

1 =
0 =

Default ()
Cancel ()
Caption ()
Font ()

Enter
Esc

True, False
True, False

UB Times, Bold, 13

16 - (Option Buttons)
, . True
False . (), , ,
.
, .
. , '
. .
Caption () ,
. Value , True False,
.
:
If optLevel.Value = True Then
MsgBox " ()"
Else
MsgBox " ( )"
End
Value default, :
If optLevel Then

17 - (Check Boxes)

.
,
.
Caption () ,
. Value , 0, 1 2,
(1=, 0=, 2= , . )..
:
Select Case chkSound.Value
Case 0
Msg = ""
Case 1
Msg = ""
Case 2
Msg = " "
End Select
MsgBox Msg
Value default, :
Select Case chkSound

18 - (Scroll Bars)
(scroll bars) (vertical) (horizontal)
, ( Value).
, . Min
Max .
LargeChange
, SmallChange
.

, Change, ,
Scroll.

19 - (List Boxes)
(list box) ' .
, .
ListCount ListIndex
. 0 ListCount-1.
, ListIndex -1. Selected .
:
iSize = lst1.ListCount-1
' lst1
For i = 0 To iSize
'
If lst1.Selected(i) = True Then
MsgBox lst1.List(i)
'
EndIf
Next

20 - (Combo Boxes)
(combo box) ' ,
.
Style Property :
0, , .
1, , .
2, , .


ListCount
ListIndex
List
Selected
Sorted




True

True

iItems=lstMyList.ListCount-1
lstMyList.ListIndex=3
sSecondItem=lstMyList.List(1)
If lstMyList.Selected(3) Then ...
lstMyList.Sorted=True

AddItem

lstMyList.AddItem ""

Clear
lstMyList.Clear


RemoveItem
lstMyList.RemoveItem 5

21 - (Arrays)
(arrays), . , Visual Basic :
Dim saNames(3) As String
saNames(0) = ""
saNames(0) = ""
saNames(0) = ""
For i = 0 To 2
Print saNames(i)
Next

' ,

'

22 - (Menus)
Visual Basic, Menu Editor, Menu
Editor... Tools Control+E .
Caption ()
' . &
, Alt ,
.
, - Caption, ,
. Name ,
mnu.
Index .
Checked, ,
. Enabled, , .
, ().
Visible, , .
,
.
Menu Editor,
Click .
:
Private Sub mnuOpen_Click()
mnuClose.Enabled = True
mnuEdit.Caption = " "
mnuPrint.Visible = True
End Sub

' Click Open


' Close
' () Edit
' Print

23 - (Timer)

Timer ()
. , , , . Timer
,
Interval .
Timer .
Enabled Timer False Interval 0, Timer.
Interval .

24 - Visual Basic
' ( ),
, , Open. Open ()
(buffer) . '
: , .
Open :
Open sPathName for Mode As #FileNumber Len=RecLength
sPathName , . ,
. , Append, Binary, Output Random.
Output,
, Append,
.
Mode . () ,
Append ( ) Input ( ) Output (
). Binary () Random ( ),
.
FileNumber , 1 511. RecLength
<=32767 .
, Close, :

Close FileNumberList
FileNumberList , .
Close , () .
Close
.

25 -
' , () ()
Line Input # Print # , . Line Input #
, Print #
.
()
, .
, ,
, , , .
, ' , ,
.
Line Input # ,
Input, (string). :
Line Input #FileNumber, sVarName
FileNumber sVarName
. ,
(enter, Chr(13)) - (Chr(13)+Chr(10)).
:
iFileNumber = 1
'
Open "C:\File01.dat" for Input As #iFileNumber
' File01.dat ()
' 10

For i=1 To 10
Line Input #iFileNumber, sStringLine$
Print sStringLine$
Next
Close #iFileNumber
'
Print # () ' ,
Output Append, . :
Print #FileNumber, OutputList
FileNumber OutputList
. OutputList, (,)
FileNumber, .
:
iFileNumber = 1
'
Open "C:\File01.dat" for Output As #iFileNumber
' File01.dat ()
' ,
' 10 ()
For i=1 To 10
Print #iFileNumber, sStringLine$
Next
Close #iFileNumber
'

26 -
(random files) ,
, ,
.
Put # () ' , Get #
. ' ,
, .

(records) (fields).
Put # :
Put # FileNumber, RecNumber, VarName
FileNumber , RecNumber VarNumber
.
Get # :
Get # FileNumber, RecNumber, VarName
FileNumber , RecNumber
VarNumber
.
:
'
Type ClientRecord
Name As String * 30
Address As String * 30
Phone As String * 10
End Type
Dim CurrentRecord As ClientRecord
Open "C:\File01.dat" for Random As #10 Len=100
' File01.dat 10
iRecordNumber = 1
'
Do While iRecordNumber < 100
...
CurrentRecord.Name = " "
CurrentRecord.Address = ". 100"
CurrentRecord.Phone = "2101234567"
...
iRecordNumber = iRecordNumber + 1
Put #10, iRecordNumber, CurrentRecord
'
...

Get #10, iRecordNumber, CurrentRecord


'
New_Name = CurrentRecord.Name
New_Address = CurrentRecord.Address
New_Phone = CurrentRecord.Phone
iRecordNumber = iRecordNumber + 1
...
Loop
Close #10
'

27 - Access Visual Basic


Data Manager Add-Ins (databases)
' , Access. Data
Manager Access, ,
. Data Manager .mdb.
(tables),
(fields) (record). Data
Manager Access.
Visual Basic Visual Basic Access,
Data .
.
Data :
Align
Caption
DatabaseName
Name
RecordSource


()


SQL '

Data, (Caption) ,

DatabaseName, , RecordSource, .
DataSource
Data , DataField ,
.
,
,
. , Data () , ,
.

You might also like