Visual Basic

94
SQA of 86 Slide 1 Visual Basic

description

Visual Basic. About the Lecturer. Age 24 Professional experience: Two years as a Developer at Intel Corp . 1 year as Web Developer in a small firm. Head of Department in Basmach (computer science school - IDF) in the field of methodology. Now at Technical System Designer in IDF - PowerPoint PPT Presentation

Transcript of Visual Basic

Page 1: Visual Basic

SQA

of 86Slide 1

Visual Basic

Page 2: Visual Basic

SQA

of 86Slide 2

About the Lecturer

โ€ข Age 24

โ€ข Professional experience:โ€ข Two years as a Developer at Intel Corp.

โ€ข 1 year as Web Developer in a small firm.

โ€ข Head of Department in Basmach (computer science school - IDF) in the field of methodology.

โ€ข Now at Technical System Designer in IDF

โ€ข Specializing in Teaching:โ€ข OO Design Principles

โ€ข .NET

โ€ข C++

Page 3: Visual Basic

SQA

of 86Slide 3

4. ืžื‘ื•ื...............................................014. ื™ืกื•ื“ื•ืช.............................................131. ืคืงื“ื™ื ื'.........................................242. ืชื ืื™ื.............................................349. ืœื•ืœืื•ืช............................................4 53. ืžืขืจื›ื™ื..........................................557. ืคื•ื ืงืฆื™ื•ืช.........................................673. ืคืงื“ื™ื............................................7

Page 4: Visual Basic

SQA

of 86Slide 4

Visual Basic

ืžื‘ื•ื

Page 5: Visual Basic

SQA

of 86Slide 5

ื”ื›ืจื•ืช ืขื ืชื›ื ื•ืช ืžื•ื ื—ื” ืืจื•ืขื™ืโ€ข

ืขื‘ื•ื“ื” ืขื ืžืžืฉืงื™ ืžืฉืชืžืฉ ื’ืจืคื™ื™ืโ€ข

VBืฉืคืช โ€ข

ืžื˜ืจืช ื”ืงื•ืจืก

Page 6: Visual Basic

SQA

of 86Slide 6

Toolbox

Project Explorerwindow

Properties window

Form

ToolbarMenu bar

ืกื‘ื™ื‘ืช ื”ืขื‘ื•ื“ื”

Page 7: Visual Basic

SQA

of 86Slide 7

Visual Basic 6ื”ืชื—ืœ ืืช ืกื‘ื™ื‘ืช ื”ืขื‘ื•ื“ื” โ€ขโ€ข Start->Programs->Microsoft Visual Studio 6.0-

>Microsoft Visual Basic 6.0Hello.exeืฆื•ืจ ืคืจื•ื™ืงื˜ ื—ื“ืฉ โ€“ โ€ข" ืฉื ืžืฆื ื‘ืืจื’ื– ื”ื›ืœื™ื ื•ื’ืจื•ืจ Buttonืœื—ืฅ ืขืœ ืคืงื“ ื”ื›ืคืชื•ืจ "โ€ข

Formืื•ืชื• ืœ-ืœื—ืฅ ืคืขืžื™ื™ื ืขืœ ื”ื›ืคืชื•ืจ ื›ื“ื™ ืœื”ืขืœื•ืช ืืช ืขื•ืจืš ื”ืงื•ื“โ€ขืจืฉื•ื ืืช ื”ืงื•ื“:โ€ข

ืœื—ืฅ ืขืœ ื›ืคืชื•ืจ ื”ื”ืคืขืœื”โ€ข

ืชื•ื›ื ื™ืช ืจืืฉื•ื ื”

Private Sub Command1_Click()MsgBox ("Hello world")End Sub

Page 8: Visual Basic

SQA

of 86Slide 8

ืฉืคื” ืžื•ื ื—ื™ืช ืืจื•ืขื™ืโ€ข

ืงืœื” ืœืฉื™ืžื•ืฉโ€ข

RAD(Rapid Applictionืžืชืื™ืžื” ืœ-โ€ขDevelopment)

VBืžื”ื™

Page 9: Visual Basic

SQA

of 86Slide 9

โ€ขVB ืžืฉืชืžืฉืช ื‘ืžื•ืฉื’ formื›ื“ื™ ืœื”ืชื›ื•ื•ืŸ ืœื—ืœื•ืŸ

frm. ื ืฉืžืจ ื‘ืงื•ื‘ืฅ formื”ืžื™ื“ืข ืขืœ โ€ข

ื‘ื”ืชืื form ืžืฆื™ื™ืจืช ืืช ื”VBื”ืžืขืจื›ืช ืฉืœ โ€ขืœืžื™ื“ืข ื”ื–ื”

OOP ื›ืžื• ืœืžื—ืœืงื•ืช ื‘Formsื ื™ืชืŸ ืœื”ืชื™ื™ื—ืก ืœ-โ€ข

Forms

Page 10: Visual Basic

SQA

of 86Slide 10

ื›ืคืชื•ืจื™ื, ืชื™ื‘ื•ืช ื˜ืงืกื˜, ืชื•ื™ื•ืช, ืชื™ื‘ื•ืช ืกื™ืžื•ืŸ.....โ€ข

= ืืœืžื ื˜ ืฉืœ ืžืžืฉืง ืžืฉืชืžืฉVBืคืงื“ ืฉืœ โ€ข

ื—ืœืงื ื‘ืœืชื™ ื ืจืื™ื (ื›ืžื• ื˜ื™ื™ืžืจ)โ€ข

ืœืคืงื“ื™ื ื™ืฉ ืžืืคื™ื™ื ื™ื (ืœื“ื•ื’ืžื ืฆื‘ืข ืจืงืข)โ€ข

ืฉืœื•ืฉื” ืกื•ื’ื™ื:โ€ขืกื˜ื ื“ืจื˜ื™ืโ€ข

)MSืœื ืกื˜ื ื“ืจื˜ื™ื ืฉืœ ื’ื•ืจื ืฆื“ ืฉืœื™ืฉื™ (ื›ืžื• โ€ข

โ€ขActiveXืฉื ื›ืชื‘ื™ื ืคื ื™ืžื™ืช

Controls

Page 11: Visual Basic

SQA

of 86Slide 11

โ€“ ืœื ืฉืคื” ืžื•ื ื—ื™ืช ืื•ื‘ื™ื™ืงื˜ื™ื VB ื‘-OOื™ืฉ ืžืขื˜ โ€ขื˜ื”ื•ืจื”

)Formื™ืฉ ืืคืฉืจื•ืช ืœื™ืฆื•ืจ ืื•ื‘ื™ื™ืงื˜ื™ื (ืœื“ื•ื’ืžื โ€ข

ืชื›ื•ื ื” = ืขืจืš ืžื™ื“ืข ืฉืžื™ื•ื—ืก ืœืื•ื‘ื™ื™ืงื˜โ€ข

ืžืชื•ื“ื” = ืžืฉื”ื• ืฉื”ืื•ื‘ื™ื™ืงื˜ ื™ื›ื•ืœ ืœืขืฉื•ืชโ€ข

ืื•ื‘ื™ื™ืงื˜ื™ื

Page 12: Visual Basic

SQA

of 86Slide 12

OOื“ื•ื’ืžื ืœ

Dim f As Form2Set f = New Form2

f.Show

f.BackColor = RGB(255, 0, 0)

Form2 is a class

f is an object โ€“ an instance of class Form2

the Form2 class has a method called show

It has a property called BackColor

Page 13: Visual Basic

SQA

of 86Slide 13

Event-driven programming

GUIื’ื™ืฉื” ืกื˜ื ื“ืจื˜ื™ืช ืœโ€ข

ื‘ื ื™ื’ื•ื“ ืœื’ื™ืฉื” ื”ื™ืฉื ื” (ื”ื“ืคืกื•ืช ืขืœ ื”ืžืกืš) ืฉื‘ื” โ€ขื”ืชื•ื›ื ื™ืช ืงื‘ืขื” ืžื” ืฆืจื™ืš ืœืงืจื•ืช

) ืžื” ื”ืืคืœื™ืงืฆื™ื” triggers ื”ืžืฉืชืžืฉ ืงื•ื‘ืข (GUIื‘โ€ขืชืขืฉื” (ื‘ืขื™ืงืจ)

): ืœื—ื™ืฆืช ืžืงืฉ, ื”ื–ื–ืช Eventsื“ื•ื’ืžืื•ืช ืœืื™ืจื•ืขื™ื (โ€ข ืฉืœ ื˜ื™ื™ืžืจtimeoutืขื›ื‘ืจ,

โ€ขEvent Handler ืžืชื•ื“ื” ืฉืชืชื‘ืฆืข ื›ืืฉืจ ื”ืื™ืจื•ืข โ€“ ืงื•ืจื”

Page 14: Visual Basic

SQA

of 86Slide 14

Visual Basic

ื™ืกื•ื“ื•ืช

Page 15: Visual Basic

SQA

of 86Slide 15

Project

ืคืจื•ื™ืงื˜ = ืงื•ื“ ื‘ืฉื‘ื™ืœ ืืคืœื™ืงืฆื™ื”โ€ข

โ€ขForm ื—ืœื•ืŸ โ€“ ื™ืชื›ื ื• ืžืกืคืจ ื‘ืคืจื•ื™ืงื˜ ืื—ื“ =

โ€ขModule ืงื•ื“ = VB

โ€ขClassื”ื’ื“ืจืช ืžื—ืœืงื•ืช ืžืฉืœ ืขืฆืžื ื• =

exeื”ืชื•ืฆืจ ืฉืœ ื”ืคืจื•ื™ืงื˜ ื”ื•ื .โ€ข

Page 16: Visual Basic

SQA

of 86Slide 16

ืงื‘ืฆื™ ืคืจื•ื™ืงื˜

Page 17: Visual Basic

SQA

of 86Slide 17

VERSION 5.00Begin VB.Form Form1 Caption = "Form1" ClientHeight = 2445 ClientLeft = 5145 ClientTop = 4110 ClientWidth = 5070 LinkTopic = "Form1" ScaleHeight = 2445 ScaleWidth = 5070 Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Left = 1200 TabIndex = 0 Top = 600 Width = 1815 EndEndAttribute VB_Name = "Form1"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption Explicit

Private Sub Command1_Click()MsgBox ("Hello world")

End Sub

Inside a .frm file

Page 18: Visual Basic

SQA

of 86Slide 18

Visual Basic

ืžืฉืชื ื™ื

Page 19: Visual Basic

SQA

of 86Slide 19

Variables

ืœื›ืœ ืžืฉืชื ื” ื‘ืชื•ื›ื ื™ืช ืžื•ืงืฆื” ื˜ื™ืคื•ืก ื ืชื•ื ื™ืโ€ข

ื›ื“ื™ ืœื”ืฆื”ื™ืจ ืขืœ ื”ื˜ื™ืคื•ืก ืฉืœ Dimื”ืฉืชืžืฉื• ื‘โ€ขื”ืžืฉืชื ื”

โ€ข Dim x as Integer

โ€ข Dim x,y as Integer NO!

โ€ขCase sensitive)ื‘ืขืจืš(

ืžืฉืคื˜ ื”ืฉืžื”โ€ข

X = 4

Page 20: Visual Basic

SQA

of 86Slide 20

Variables

ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ืžืฉืชื ื” ื’ื ืื ืœื ื”ื’ื“ืจื ื• VBื‘โ€ขืื•ืชื• (ืœื ืžื•ืžืœืฅ) ืืœื ืื ื ื•ืกื™ืฃ ืืช ื”ืคืงื•ื“ื”

Option Explicit

ื”ื’ื“ืจืช ืงื‘ื•ืขื™ืโ€ข

Private const MyInt As Integer = 5

Stringื›ื“ื™ ืœื”ื—ื–ื™ืง ืžื—ืจื•ื–ื•ืช ื”ืฉืชืžืฉื• ื‘ื˜ื™ืคื•ืก โ€ข

Dim flower As String

Page 21: Visual Basic

SQA

of 86Slide 21

ื˜ื™ืคื•ืกื™ื

โ€ข Integer Long โ€ข Single Double โ€ข Currencyโ€ข Byte unsigned 0 - 255โ€ข Stringโ€ข Booleanโ€ข Dateโ€ข Objectโ€ข Variant - do not use except explicit reason

Page 22: Visual Basic

SQA

of 86Slide 22

Variant

ื™ื›ื•ืœ ืœื”ื›ื™ืœ ื›ืœ ืกื•ื’ ืฉืœ ืžื™ื“ืขVariantื”ื˜ื™ืคื•ืก โ€ข

ื ื™ืชืŸ ืœื“ืขืช ืžื” ื˜ื™ืคื•ืก ื”ืžื™ื“ืข ื‘ื–ืžืŸ ืจื™ืฆื” ืข"ื™ โ€ขVarTypeDim x As Variant

x = 1MsgBox (VarType(x))

Page 23: Visual Basic

SQA

of 86Slide 23

VarTypeืขืจื›ื™ 0 Empty (uninitialized)1 Null (no valid data)2 Integer3 Long integer4 Single-precision floating-point number5 Double-precision floating-point number6 Currency value7 Date value8 String9 Object10 Error value11 Boolean value12 Variant (used only with arrays of variants)13 A data access object14 Decimal value17 Byte value36 Variants that contain user-defined types8192 Array

Page 24: Visual Basic

SQA

of 86Slide 24

Operator operation Basic expression

^ Exponentiation A ^ B

* Multiplication A * B

/ Division A / B

+ Addition A + B

- Subtraction A - B

ืคืขื•ืœื•ืช ืืจื™ืชืžื˜ื™ื•ืช

Page 25: Visual Basic

SQA

of 86Slide 25

ืฉืจืฉื•ืจ

ื ื™ืชืŸ ืœืฉืจืฉืจ ืฉืชื™ ืžื—ืจื•ื–ื•ืช ื‘ืขื–ืจืช ืื•ืคืจื˜ื•ืจ ื”ืฉืจืฉื•ืจ โ€ข&

Private Sub cmdConcatenation_Click()Dim strVar1 As StringDim strVar2 As StringDim result As StringstrVar1 = โ€œHelloโ€strVar2 = โ€œWorldโ€result = strVar1 & strVar2

End Sub

Page 26: Visual Basic

SQA

of 86Slide 26

HEXื”ืขืจื•ืช, ื”ืžืฉืš ืฉื•ืจื” ื•

ื”ืขืจื•ืช ืžืชื—ื™ืœื•ืช ืขื ื’ืจืฉ ', ื•ืžืžืฉื™ื›ื•ืช ืขื“ ืœืกื•ืฃ โ€ขื”ืฉื•ืจื”

'this line is a comment

ื‘ื™ื˜ื•ื™ ืืจื•ืš ืžืื•ื“ ื ื™ืชืŸ ืœืคืฆืœ ืœืฉื•ืจื•ืช ื‘ืขื–ืจืช โ€ขืงื•-ืชื—ืชื•ืŸ _

ื›ื“ื™ ืœื”ืžืฉื™ืš ืœืฉื•ืจื” ื”ื‘ืื”

HFF0012&ืงื‘ื•ืขื™ื ื”ืงืกื”-ื“ืฆื™ืžืœื™ื ื ื›ืชื‘ื™ื ื‘ืฆื•ืจื”: โ€ข

Page 27: Visual Basic

SQA

of 86Slide 27

Visual Basic

ื”ืžืจื•ืช

Page 28: Visual Basic

SQA

of 86Slide 28

Conversion function Converts an expression to

Cbool Boolean

Cbyte Byte

Ccur Currency

Cdate Date

CDbl Double

Cint Integer

CLng Long

CSng Single

CStr String

Cvar Variant

CVErr Error

DIM x as integerx = Cint("10")

ื”ืžืจื•ืช

Page 29: Visual Basic

SQA

of 86Slide 29

Form properties

Page 30: Visual Basic

SQA

of 86Slide 30

Controls and properties

Page 31: Visual Basic

SQA

of 86Slide 31

Visual Basic

1ืคืงื“ื™ื โ€“ ื—ืœืง

Page 32: Visual Basic

SQA

of 86Slide 32

ื”ืคืงื“ื™ื ื”ืฉื™ืžื•ืฉื™ื™ื ื‘ื™ื•ืชืจ

โ€ข Text Boxes

โ€ข Labels

โ€ข Command Buttons

โ€ข Picture Boxes

Page 33: Visual Basic

SQA

of 86Slide 33

TextBox

ืœื—ืฆื• ืคืขืžื™ื™ื ืขืœ ืชื™ื‘ืช ื”ื˜ืงืกื˜ ื‘ืืจื’ื– ื”ื›ืœื™ื ื›ื“ื™ โ€ขืœื”ื•ืกื™ืฃ ืœื—ืœื•ืŸ

)F4ื”ืคืขื™ืœื• ืืช ื—ืœื•ืŸ ื”ืžืืคื™ื™ื ื™ื (ืข"ื™ ืœื—ื™ืฆื” ืขืœ โ€ข

ื”ืฆื™ื‘ื• ืขืจื›ื™ื ื‘ืžืืคื™ื™ื ื™ื ืฉืœ ืชื™ื‘ืช ื”ื˜ืงืกื˜โ€ข

Page 34: Visual Basic

SQA

of 86Slide 34

TextBox

Text box

Page 35: Visual Basic

SQA

of 86Slide 35

ืžืืคื™ื™ื ื™ื ื—ืฉื•ื‘ื™ื

โ€ข Name

โ€ข Caption

โ€ข Border style

โ€ข Visible

โ€ข Back Color

โ€ข Alignment

โ€ข Font

Page 36: Visual Basic

SQA

of 86Slide 36

.ืžื—ืจื•ื–ืชื™ื”ืชื•ื›ืŸ ืฉืœ ืชื™ื‘ื•ืช ื˜ืงืกื˜ ื”ื•ื ืชืžื™ื“ โ€ข

ื’ื ืžืกืคืจื™ื ืžืื•ื—ืกื ื™ื ื‘ืชื™ื‘ื•ืช ื˜ืงืกื˜ ื‘ืชื•ืจ โ€ขืžื—ืจื•ื–ืช.

ืžื›ืืŸ ื ื•ื‘ืข ืฉืฆืจื™ืš ืœื”ืžื™ืจ ืืช ื”ืžื—ืจื•ื–ืช ืฉืœ ืชื™ื‘ืช โ€ขื”ื˜ืงืกื˜ ืœืคื ื™ ืฉืžืฉืชืžืฉื™ื ื‘ื” ื›ืžืกืคืจ

โ€ขVal (txtBox.Text) ืžืฉื ื” ืืช ืžื—ืจื•ื–ืช ื”ืงืœื˜ ืœืžืกืคืจ

numVar = Val (txtBox.Text)

txtBox.Text = Str(numVar)

ืฉื™ืžื•ืฉ ื‘ืชื™ื‘ื•ืช ื˜ืงืกื˜ ืœืงืœื˜ ื•ืคืœื˜

Page 37: Visual Basic

SQA

of 86Slide 37

Object Prefix Example

Command Button cmd cmdStart

Form frm frmPayroll

Label lbl lblName

Picture box pic picClouds

Text box txt txtAddress

ื—ื•ืงื™ื ื‘ื ืชื™ื ืช ืฉืžื•ืช

Page 38: Visual Basic

SQA

of 86Slide 38

Private Sub Command1_Click()Dim num1 As IntegerDim num2 As IntegerDim result As Integernum1 = val(Text1.Text)num2 = val(Text2.Text)result = num1 + num2Label1.Caption = str(result)End Sub

Example - calculator

Page 39: Visual Basic

SQA

of 86Slide 39

ืชืจื’ื™ืœ

ื”ื•ืกื™ืคื• ืœืžื—ืฉื‘ื•ืŸ ืชืžื™ื›ื” ื‘ื—ื™ืกื•ืจ, ื›ืคืœ, ื•ื—ื™ืœื•ืงโ€ข

ื”ืชืขืœืžื• ืžืกืคืจื™ื ืœื ื—ื•ืงื™ื™ื ื•ื—ืœื•ืงื” ื‘ืืคืก โ€“ โ€ขื ืชื™ื™ื—ืก ืœื–ื” ื‘ื”ืžืฉืš

Page 40: Visual Basic

SQA

of 86Slide 40

ื‘ืžืงื•ื input boxืขื‘ื•ืจ ืงืœื˜ ื‘ื•ื“ื“ ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘โ€ขืชื™ื‘ืช ื˜ืงืกื˜

โ€ขInput Box ื”ื•ื ืชื™ื‘ืช ื“ื™ืืœื•ื’ ืžื•ื’ื“ืจืช ืžืจืืฉ

โ€ข Input Box is a predefined dialog box.

ืชื—ื‘ื™ืจ:โ€ข

stringVar = InputBox (prompt, title)

ืงืœื˜ ืžืชื™ื‘ืช ืงืœื˜

Page 41: Visual Basic

SQA

of 86Slide 41

Private Sub cmdDisplay_Click()

Dim fileName As String

fileName = InputBox("Enter the name of the file containing the information.", "Name of File")

End Sub

After executing an inputbox would pop up

Input Boxื“ื•ื’ืžื ืœ-

Page 42: Visual Basic

SQA

of 86Slide 42

Visual Basic

ืชื ืื™ื

Page 43: Visual Basic

SQA

of 86Slide 43

If c > 5 Then x = 1 y = 3End If

If c > 5 Then x = 1: y = 3

If c > 5 Then x = 1 y = 3Else z = 7End If

If c > 5 Then x = 1 y = 3ElseIf c = 4 Then z = 7Else x = 9End If

IFืžืฉืคื˜

Page 44: Visual Basic

SQA

of 86Slide 44

ืชืจื’ื™ืœ โ€“ ืคื™ืชืจื•ืŸ ืžืฉื•ื•ืื” ืจื™ื‘ื•ืขื™ืช

ืขื:formืฆืจื• โ€ข a,b,c ืชื™ื‘ื•ืช ื˜ืงืกื˜ ืฉื™ื›ื™ืœื• ืืช ืขืจื›ื™ 3โ€ข

ืชื•ื™ื•ืช ืฉื‘ื”ืŸ ื”ืชื•ืฆืื•ืช ื™ื•ืฆื’ื•2โ€ข

ื›ืคืชื•ืจ ื‘ืฉื "ืคืชื•ืจ" โ€“ ื”ืงื•ื“ ืฉืžืฉื•ื™ืš ืœื›ืคืชื•ืจ ื™ืคืชื•ืจ ืืช ื”ืžืฉื•ื•ืื”โ€ข

sqr* ืคื•ื ืงืฆื™ื” ืœืฉื•ืจืฉ ื”ื™ื

a

acbbx

2

42

Page 45: Visual Basic

SQA

of 86Slide 45

Dim Number Number = 8 ' Initialize variable. Select Case Number ' Evaluate Number.

Case 1 To 5 ' Number between 1 and 5, inclusive. x=4Case 6, 20, 8 ' Number is 6 or 20 or 8. x=5Case 9 To 10 ' Number is 9 or 10. x=6Case is<0X=7Case Else ' Other values. x=8

End Select

select

Page 46: Visual Basic

SQA

of 86Slide 46

Visual Basic

ื˜ื™ืคื•ืœ ื‘ืฉื’ื™ืื•ืช

Page 47: Visual Basic

SQA

of 86Slide 47

โ€ขExceptionื‘ืขื™ื” ืฉืขื•ืœื” ื‘ื–ืžืŸ ืจื™ืฆื” =

I/O ื‘ื“"ื› ืงืฉื•ืจื” ืœ-โ€ข

ืœื“ื•ื’ืžื:ืงื•ื‘ืฅ ืฉืœื ื ืžืฆื, ื‘ืขื™ืช ื—ื™ื‘ื•ืจ ืœืฉืจืช, โ€ขืงืœื˜ ืฉื’ื•ื™ ืฉืœ ื”ืžืฉืชืžืฉ

ืœื ื‘ืื’ ืชื™ื›ื ื•ืชื™โ€ข

โ€ขVBืงื•ืจื ืœ exception- ื‘ืฉื error

Error and exception handling

Page 48: Visual Basic

SQA

of 86Slide 48

Private Sub Command1_Click()Dim num1 As IntegerDim num2 As IntegerDim result As Integer

On Error GoTo myErrorHandlernum1 = Text1.Textnum2 = Text2.Textresult = num1 + num2Label1.Caption = resultExit Sub

myErrorHandler:If Err.Number = 13 Then MsgBox ("Please enter a valid number")Else MsgBox (Err.Description)End IfResume Next

End Sub

ืชืจื’ื™ืœ

ื”ื•ืกื™ืคื• ื˜ื™ืคื•ืœ ื‘ืฉื’ื™ืื•ืช ื‘ืžื—ืฉื‘ื•ืŸ ืฉืœื›ื

(11ื˜ืคืœื• ื‘ื‘ืขื™ืช ื”ื—ืœื•ืงื” ื‘ืืคืก)

ื“ื•ื’ืžื ืœื˜ื™ืคื•ืœ ื‘ืฉื’ื™ืื•ืช โ€“ ืžืกืคืจื™ื ืœื ื—ื•ืงื™ื™ื

Page 49: Visual Basic

SQA

of 86Slide 49

Visual Basic

ืœื•ืœืื•ืช

Page 50: Visual Basic

SQA

of 86Slide 50

Dim x as Integer, total As Integertotal = 0For x = 1 To 5 total = total + xNext

Dim x as Integer, total As Integertotal = 0For x = 1 To 5 Step 2 total = total + xNext

For next loops

Page 51: Visual Basic

SQA

of 86Slide 51

Do While name<>โ€ืกื•ืฃโ€name = InputBox)โ€œื”ืงืœื“ ืืช ืฉืžืš ืื• ืกื•ืฃ ื›ื“ื™ ืœืฆืืชโ€(if name<>โ€ืกื•ืฃโ€ Then Print name

Loop

Do while

Do name = InputBox)โ€œื”ืงืœื“ ืืช ืฉืžืš ืื• ืกื•ืฃ ื›ื“ื™ ืœืฆืืชโ€(if name<>โ€ืกื•ืฃโ€ Then Print name

Loop While name<>โ€ืกื•ืฃโ€

Page 52: Visual Basic

SQA

of 86Slide 52

Dim c As Integerc = 1Do While c < 5 c = c + 1Loop

Dim c As Integerc = 1Do Until c >4 c = c + 1Loop

Dim c As Integerc = 1Do c = c + 1Loop While c < 5

Dim c As Integer, x as integerc = 1x=2Do c = c + 1Loop Until c>4 And x<>3

ืœื•ืœืื•ืช ื ื•ืกืคื•ืช

Page 53: Visual Basic

SQA

of 86Slide 53

Visual Basic

ืžืขืจื›ื™ื

Page 54: Visual Basic

SQA

of 86Slide 54

Dim x(100) As IntegerDim i As IntegerFor i = 0 To 100 x(i) = 99Next

Dim x(1 To 3, 1 To 3) As IntegerDim i as integer, j As IntegerFor i = 1 To 3 For j = 1 To 3 x(i, j) = 99 NextNext

ืžืขืจื›ื™ื ื‘ืขืœื™ ื’ื•ื“ืœ ืงื‘ื•ืข

Page 55: Visual Basic

SQA

of 86Slide 55

Dim x() As IntegerReDim x(5)

Dim i, j As IntegerFor i = 1 To 5 x(i) = 99Next

ReDim Preserve x(10)For i = 6 To 10 x(i) = 100Next

ืžืขืจื›ื™ื ื“ื™ื ืžื™ื™ื

ืื•ืžืจ ืœืžืขืจื›ืช

ืœืฉืžื•ืจ ืืช ื”ืขืจื›ื™ื ืฉื”ื™ื•

Page 56: Visual Basic

SQA

of 86Slide 56

โ€ข Debug.print x,y,z

โ€ข Immediate window โ€“ CTRL G

โ€“ ืชืจื’ื™ืœื›ืชื‘ื• ืชื•ื›ื ื™ืช ืฉืชื“ืคื™ืก ื‘ืืžืฆืขื•ืช ืœื•ืœืืช

FOR ืืช ื”ืกื›ื•ื ืฉืœ ื›ืœ ื”ืžืกืคืจื™ื . ื”ืฉืชืžืฉื• ื‘ื”ื“ืคืกืช 9 ืขื“ 1ื”ืื™-ื–ื•ื’ื™ื™ื ืž

Debug

Debugging โ€“ debug.print

Page 57: Visual Basic

SQA

of 86Slide 57

Visual Basic

ืกื‘ืจื•ื˜ื™ื ื•ืช ื•ืคื•ื ืงืฆื™ื•ืช

Page 58: Visual Basic

SQA

of 86Slide 58

Private Sub Command1_Click)(Dim x, y As Doublex = 1y = 2Debug.Print addUp)x, y(

End Sub

Private Function addUp)a as double, b As Double(addUp = a + bEnd Function

x and y copied to a and b

ืคื•ื ืงืฆื™ื•ืช

Page 59: Visual Basic

SQA

of 86Slide 59

Rnd Returns a number between 0 and 1.(excluding 1)

Sqr(n) Returns the square root of a number.

Round(n,r) The number n is rounded to r decimalplaces.

Int(n) Returns the largest integer less thanor equal to a number

ืคื•ื ืงืฆื™ื•ืช ื ื•ืžืจื™ื•ืช

Page 60: Visual Basic

SQA

of 86Slide 60

Private Sub cmdEvaluate_Click()

Dim n As Single

Dim root As Single

picResults.Cls

n = 6.76

root = Sqr(n)

picResults.Print root; Int(n); Round(n,1)

End Sub

ื“ื•ื’ืžื

Page 61: Visual Basic

SQA

of 86Slide 61

Private Sub Command1_Click()Dim x As Double, y As Doublex = 1y = 200Call bigger(x, y)End Sub

Private Sub bigger(first As Double, second As Double)If first > second Then Debug.Print firstElse Debug.Print secondEnd IfEnd Sub

Functions return values

Procedures do not โ€“ they just do something

ืกื‘ืจื•ื˜ื™ื ื•ืช = ืคื•ื ืงืฆื™ื•ืช ืœืœื ืขืจืš ื”ื—ื–ืจ

Page 62: Visual Basic

SQA

of 86Slide 62

ื ื™ืชืŸ ืœื”ืขื‘ื™ืจ ืคืจืžื˜ืจื™ื ื‘ืฉืชื™ ืฆื•ืจื•ืชโ€ขโ€ขBy value

โ€ขBy reference

ื”ืขื‘ืจืช ืคืจืžื˜ืจื™ื

Page 63: Visual Basic

SQA

of 86Slide 63

ืžืขืชื™ืงื™ื ืืช By Valueืคืจืžื˜ืจื™ื ื”ืžื•ืขื‘ืจื™ื โ€ขื”ืชื•ื›ืŸ ืฉืœื”ืŸ ืœืคื•ื ืงืฆื™ื”

ืฉื™ื ื•ื™ ืขืจืš ื‘ืคื•ื ืงืฆื™ื” ืœื ืžืฉืคื™ืข ืขืœ ื”ืžืฉืชื ื™ื โ€ขื”ืžืงื•ืจื™ื™ื

ื™ื•ืฆืจ ืžืฉืชื ื™ื ื ื•ืกืคื™ื โ€“ ื“ื‘ืจ ื”ืžืฉืคื™ืข ืขืœ ื”ื–ื™ื›ืจื•ืŸ โ€ข(ืžืฉืžืขื•ืชื™ ื‘ืžืขืจื›ื™ื ื’ื“ื•ืœื™ื)

VBื–ื•ื”ื™ ื‘ืจื™ืจืช ื”ืžื—ื“ืœ ื‘โ€ข

By valueื”ืขื‘ืจืช ืคืจืžื˜ืจื™ื -

Page 64: Visual Basic

SQA

of 86Slide 64

ื”ื›ืชื•ื‘ืช ืฉืœ ื”ืคืจืžื˜ืจื™ื ืžื•ืขื‘ืจื™ื ืœืคื•ื ืงืฆื™ื”โ€ข

ืฉื™ื ื•ื™ ืฉืœ ืขืจืš ื‘ืคื•ื ืงืฆื™ื” ื’ื•ืจื ืœืฉื™ื ื•ื™ ื‘ืžืฉืชื ื™ื โ€ขื”ืžืงื•ืจื™ื™ื

ื—ื•ืกืš ื‘ื–ื™ื›ืจื•ืŸ ื•ืžื”ื™ืจ ื™ื•ืชืจโ€ข

ืžืกื•ื›ืŸ ื™ื•ืชืจโ€ข

ื›ื“ื™ ืœื”ืฉืชืžืฉ ื‘ืฉื™ื˜ื” ื–ื• ื™ืฉ ืœืจืฉื•ื ืœื™ื“ ื”ืคืจืžื˜ืจ โ€ขByRef

By referenceื”ืขื‘ืจืช ืคืจืžื˜ืจื™ื -

Page 65: Visual Basic

SQA

of 86Slide 65

Private Sub Command1_Click)(Dim x As Double, y As Doublex = 1y = 200Call swap)x, y(Debug.Print x, yEnd Sub

Private Sub swap)ByRef first As Double, ByRef second As Double(Dim temp As Doubletemp = firstfirst = secondsecond = tempEnd Sub

by referenceื”ืขื‘ืจื”

Page 66: Visual Basic

SQA

of 86Slide 66

Private Sub Command1_Click()Dim x As Double, y As Doublex = 1y = 2Debug.Print addUp(second:=x, first:=y)End Sub

Private Function addUp(first As Double, second As Double)addUp = firstEnd Function

Named arguments

Page 67: Visual Basic

SQA

of 86Slide 67

Visual Basic

ืžื•ื“ื•ืœื™ื

Page 68: Visual Basic

SQA

of 86Slide 68

ืงื‘ืฆื™ื ื ืคืจื“ื™ืโ€ข

ืžื›ื™ืœื™ื ืคื•ื ืงืฆื™ื•ืช, ืคืจื•ืฆื“ื•ืจื•ืช ื•ืžืฉืชื ื™ื ื’ืœื•ื‘ืœื™ืโ€ข

ืื—ื“formืžืฉืžืฉ ืœืงื•ื“ ื•ืžื™ื“ืข ืฉื–ืงื•ืงื™ื ืœื• ื‘ื™ื•ืชืจ ืžโ€ข

Modules

Page 69: Visual Basic

SQA

of 86Slide 69

Module example

Page 70: Visual Basic

SQA

of 86Slide 70

ืžืฉืชื ื™ื ืฆืจื™ื›ื™ื ืœื”ื™ื•ืช ืœื•ืงืœื™ื™ื ืขื“ ื›ืžื” ืฉืืคืฉืจ

Scope ื—ืœืง ื‘ืชื•ื›ื ื™ืช ืฉื‘ื• ื ื™ืชืŸ :ืœืคื ื•ืช ืœืžืฉืชื ื”

Scope โ€“ public private dim

Page 71: Visual Basic

SQA

of 86Slide 71

โ€ขDIMืœื•ืงืœื™ ืœืกื‘ืจื•ื˜ื™ื ื” โ€“ ื›ืœ ืขื•ื“ ื”ืกื‘ืจื•ื˜ื™ื ื” ืจืฆื” โ€“

โ€ขPrivte\Public ืขืœ FORMื›ืœ ืขื•ื“ ื” โ€“ FORMืคืชื•ื—

โ€ขPublicื‘ืžื•ื“ื•ืœ โ€“ ื›ืœ ืขื•ื“ ื”ืืคืœื™ืงืฆื™ื” ืจืฆื”

โ€ขStatic ืœื•ืงืœื™ ืœืกื‘ืจื•ื˜ื™ื ื” โ€“ ืฉื•ืžืจ ืขืจื›ื• ื‘ื™ืŸ ืจื™ืฆื•ืช โ€“ ืฉืœ ื”ืกื‘ืจื•ื˜ื™ื ื” โ€“ ื—ื™ ืขื“ ืกื•ืฃ ื”ืชื•ื›ื ื™ืช

ื–ืžืŸ ื—ื™ื™ื โ€“ ื›ืžื” ื–ืžืŸ ืžืฉืชื ื” ื—ื™

Page 72: Visual Basic

SQA

of 86Slide 72

Visual Basic

ืคืงื“ื™ื - ื—ืœืง ื‘'

Page 73: Visual Basic

SQA

of 86Slide 73

ื™ื™ืฉื•ืจ ื”ืคืงื“ื™ื

Page 74: Visual Basic

SQA

of 86Slide 74

Scrollbar control

Page 75: Visual Basic

SQA

of 86Slide 75

Label1

ScrollBar1

Label2 Picture1

Private Sub VScroll1_Change()Picture1.BackColor = RGB(0, 0, VScroll1.Value)Label2.Caption = "Colour is 0, 0, " & VScroll1.ValueEnd Sub

Scrollbarื“ื•ื’ืžื ืœืฉื™ืžื•ืฉ ื‘-

Page 76: Visual Basic

SQA

of 86Slide 76

ื ื•ืกืคื™ื ืœื™ืจื•ืง ื•ืื“ื•ืscrollbarsื”ื•ืกื™ืคื• โ€ข

ืชืจื’ื™ืœ

Page 77: Visual Basic

SQA

of 86Slide 77

Radio buttons

Page 78: Visual Basic

SQA

of 86Slide 78

Checkboxes

Page 79: Visual Basic

SQA

of 86Slide 79

Listbox

Page 80: Visual Basic

SQA

of 86Slide 80

Tab Control ื”ื•ืกืคื” -

Page 81: Visual Basic

SQA

of 86Slide 81

Tab Control ืฉื™ืžื•ืฉ -

Page 82: Visual Basic

SQA

of 86Slide 82

โ€ข Hidden control

โ€ข Interval

โ€ข Tick event

โ€ข Enabled or not

Timer

Page 83: Visual Basic

SQA

of 86Slide 83

Private Sub Timer1_Timer()Dim currentTime As DatecurrentTime = Now()Label1.Caption = currentTimeEnd Sub

ื“ื•ื’ืžื ืœื˜ื™ื™ืžืจ - ืฉืขื•ืŸ

Page 84: Visual Basic

SQA

of 86Slide 84

ื›ืชื‘ื• ืชื•ื›ื ื™ืช ืฉืขื•ืฉื” ืื ื™ืžืฆื™ื” ืฉืœ ื›ื“ื•ืจ ืฉื–ื– ืขืœ โ€ขื”ืžืกืš

ื•ื˜ื™ื™ืžืจshapeื”ืฉืชืžืฉื• ื‘ืคืงื“ ืฉืœ โ€ข

ื”ื–ื™ื–ื• ืืช ื”ืฆื•ืจื”:tickื›ืœ ืคืขื ืฉื™ืฉ โ€ข

Shape.left โ€“ shape.lest+speed

ืชืจื’ื™ืœ ื˜ื™ื™ืžืจ

Page 85: Visual Basic

SQA

of 86Slide 85

ื ื™ืชืŸ ืœืฆืจืฃ ืžืกืคืจ ืคืงื“ื™ื ืžืื•ืชื• ืกื•ื’ ื›ืžืขืจืš โ€ขืคืงื“ื™ื

ื‘ืฆื•ืจื” ื›ื–ื• ื ื•ื›ืœ ืœืฉืชืฃ ืงื•ื“ ืฉื™ื”ื™ื” ืชืงืฃ ืœื›ื•ืœื โ€ขื•ื ื•ื›ืœ ืœื–ื”ื•ืช ืื™ื–ื” ื”ื•ืคืขืœ ืข"ื™ ืื™ื ื“ืงืก.

ื›ื“ื™ ืœื™ืฆื•ืจ ืžืขืจืš ืคืงื“ื™ื ื™ืฉ ืœื”ืขืชื™ืง ื•ืœื”ื“ื‘ื™ืง โ€ขืืช ื”ืคืงื“ ื”ืจืืฉื•ืŸ

ืžืขืจืš ืคืงื“ื™ื

Page 86: Visual Basic

SQA

of 86Slide 86

Backup

Page 87: Visual Basic

SQA

of 86Slide 87

ืžืืคืฉืจืช ืœืฉื™ื ืชืžื•ื ื” ื‘ื—ืœื•ืŸ ืฉืœื ื•โ€ขืžืืคื™ื™ื ื™ื ื—ืฉื•ื‘ื™ืโ€ข

โ€ขPictureืžื›ื™ืœ ืืช ืฉื ื”ืงื•ื‘ืฅ ืฉื™ืฉ ืœื”ืฆื™ื’ โ€“

โ€ขSrrech ืื ืฉืœื™ืœื™ ื”ืชื™ื‘ื” ืชืชืื™ื ืืช ืขืฆืžื” ืœืชืžื•ื ื” ื•ืื โ€“ ื—ื™ื•ื‘ื™ ื”ืชืžื•ื ื” ืชืชืื™ื ืืช ืขืฆืžื” ืœืชื™ื‘ื”

ืžืืคืฉืจืช ืœื ื• ืœื˜ืขื•ืŸ ืชืžื•ื ื” LoadPictureืžืชื•ื“ืช โ€ข

ืชื™ื‘ืช ืชืžื•ื ื”

Page 88: Visual Basic

SQA

of 86Slide 88

ื”ืชื—ืœ ืคืจื•ื™ืงื˜ ื—ื“ืฉ ื•ื”ื•ืกืฃ ืœืžืกืš ืชื™ื‘ืช ืชืžื•ื ื” ืื—ืช1)

ื›ืชื•ื‘ืช ืฉืœ ืงื•ื‘ืฅ ืชืžื•ื ื” Pictureื”ืฆื‘ ื‘ืชื•ืš ื”ืžืืคื™ื™ืŸ 2) C:\Program Files\Microsoft(ื ื™ืชืŸ ืœืžืฆื•ื ืชืžื•ื ื•ืช ื‘

Visual Studio\Common\Graphics\Bitmaps(

ื‘ื“ื•ืง ืžื” ืงื•ืจื” ื›ืืฉืจ ืžื•ืชื—ื™ื ืืช ืชื™ื‘ืช ื”ืชืžื•ื ื”3)

ืชื™ื‘ืช ืชืžื•ื ื” - ื“ื•ื’ืžื

Page 89: Visual Basic

SQA

of 86Slide 89

ืžืืคืฉืจ ืœืžืฉืชืžืฉื™ื ืœื‘ื—ื•ืจ ื›ื•ื ืŸ ื‘ื–ืžืŸ ืจื™ืฆื”โ€ข

ืžืืคื™ื™ื ื™ื ื—ืฉื•ื‘ื™ืโ€ขโ€ขDriveื›ื•ืœืœ ืืช ืฉื ื”ื›ื•ื ืŸ ืฉื›ืจื’ืข ื ื‘ื—ืจ โ€“

ืคืงื“ ืจืฉื™ืžืช ื›ื•ื ื ื™ื

Page 90: Visual Basic

SQA

of 86Slide 90

ืžืฆื™ื’ ืœืžืฉืชืžืฉื™ื ืจืฉื™ืžื” ืฉืœ ืชื™ืงื™ื•ืชโ€ข

ืžืืคื™ื™ื ื™ื ื—ืฉื•ื‘ื™ืโ€ขโ€ขPathื”ื›ืชื•ื‘ืช ืฉืœ ื”ืชื™ืงื™ื” ื”ื ื•ื›ื—ื™ืช ืฉืื ื—ื ื• ืขื•ื‘ื“ื™ื ื‘ื” โ€“

ืคืงื“ ืจืฉื™ืžืช ืชื™ืงื™ื•ืช

Page 91: Visual Basic

SQA

of 86Slide 91

ืžืฆื™ื’ ืœืžืฉืชืžืฉื™ื ืจืฉื™ืžื” ืฉืœ ืงื‘ืฆื™ืโ€ข

ืžืืคื™ื™ื ื™ื ื—ืฉื•ื‘ื™ืโ€ขโ€ขPathื”ื›ืชื•ื‘ืช ืฉืœ ื”ืชื™ืงื™ื” ื”ื ื•ื›ื—ื™ืช ืฉืื ื—ื ื• ืขื•ื‘ื“ื™ื ื‘ื” โ€“

โ€ขFileNameืฉื ื”ืงื•ื‘ืฅ ื”ื ื‘ื—ืจ โ€“

โ€ขPatternืžื—ืจื•ื–ืช ืขื ืชื‘ื ื™ืช ืฉืœ ืงื‘ืฆื™ื ืฉื™ืฉ ืœื”ืฆื™ื’ โ€“ dat ื™ืฆื™ื’ ืจืง ื‘ืฆื™ื ืขื ืกื™ื•ืžืช dat*.ืœื“ื•ื’ืžื:

ืคืงื“ ืจืฉื™ืžืช ืงื‘ืฆื™ื

Page 92: Visual Basic

SQA

of 86Slide 92

dirExampleื ื ื™ื— ื•ื™ืฉ ืœื ื• ืคืงื“ ืชื™ืงื™ื•ืช ื‘ืฉื โ€ข drvExample ืคืงื“ ื›ื•ื ื ื™ื ื‘ืฉื

filExampleื•ืคืงื“ ืงื‘ืฆื™ื ื‘ืฉื

โ€ข dirExample.Path = drvExample.Driveโ€ข filExample.Path = dirExample.Pathโ€ข filExample.Path = dirExample.Path

ืกื™ื ื›ืจื•ืŸ ื”ื›ื•ื ื ืŸ, ืชื™ืงื™ื” ื•ืงื•ื‘ืฅ

Page 93: Visual Basic

SQA

of 86Slide 93

Dim YourFile as String If Right(filExample.Path,1) = "\" Then YourFile = filExample.Path + filExample.FileName Else YourFile = filExample.Path + "\" + filExample.FileName End If imgExample.Picture

ื‘ื—ื™ืจืช ืชืžื•ื ื” ื‘ืฆื•ืจื” ื“ื™ื ืžื™ืช

Page 94: Visual Basic

SQA

of 86Slide 94

ืฆื•ืจ ืชื•ื›ื ื™ืช ืฉืชืชืŸ ืืคืฉืจื•ืช ืœื‘ื—ื•ืจ ืชืžื•ื ื” ื•ืชืฆื™ื’ ืื•ืชื”

ืชืจื’ื™ืœ โ€“ ื”ืฆื’ืช ืชืžื•ื ื•ืช