You are on page 1of 77

6

www.B4C4.com

.
. .
.
.
. .
.

http://www.b4c4.com

Vb .
.
. Interpreter vb
0 # "1 . Enter
syntax
.
:
"1 ) (Text base c c++

"2 ) (Graphical base vb
vb . .
... .
Vb . vb ) (IDE .
""Integrated Development Environment
IDE .
.
Vb .

35 .

80 90

vb .

vb vb . vb
. vb
.

http://www.b4c4.com

.
.
. .
.
.

vb

vb

#
!".

vb Option Explicit "Declaration #

Option Explicit .
.

vb Option Explicit

Require Variable Declaration Tools/Options .


vb .
vb Dim 0
]Dim Varname [As Datatype
Varname . a # z
" Underline Underline . 255
..Sum , Min_Max , m6 0
Datatype vb . 12 .

Byte

1 byte

0 255

Boolean

2 bytes

True False

http://www.b4c4.com

Integer

2 bytes

Long

4 bytes

-2,147,483,648 2,147,483,647

Single

4 bytes

-3.402823E38 -1.401298E-45
1.401298E-45 3.402823E38

# "

Double

8 bytes

-1.79769313486232E308 -4.94065645841247E-324
4.94065645841247E-324 1.79769313486232E308

# "

Currency

-32,768 32,767

8 bytes

-922,337,203,685,477.5808 922,337,203,685,477.5807

)(Scaled integer

Decimal

14 bytes

+/-79,228,162,514,264,337,593,543,950,335

+/-7.9228162514264337593543950335 28

1 +/-0.0000000000000000000000000001

Date

8 bytes

Object

4 bytes

#String "

#String "

100 31 9999

Any Object reference

# 0 "10 bytes $

# Variant" 16 bytes

1 65400

Double

# Variant" # $

"22 bytes

http://www.b4c4.com

0
.1 .
.

.2 As Datatype .

Optional

. As Datatype vb Variant #
Variant " . 0
Dim x as Variant
Dim x
.3 . vb " "Double Quotation .
# " .

vb

Null String .
vbNullString vb .

.4 String . vb
0
Dim Str_name as String
Str_name # " . .
0
Dim Str_name as String * len
Str_name len .0
Dim Name1 as String * 10

http://www.b4c4.com

Name1 10 .

10

Name1 vb 10 Name1 .

.5 ) (True,False .

vb .6 Date . # .0
Dim dt as Date
dt = #1/31/2009#
dt = #11:18:12 AM#
.7 Dim .
As Datatype .0
Dim Total, sum as Integer
Dim Name1 as String, i as Integer, check as Boolean
.8 Currency .

.9 Single .

.10 Byte .

:
Byte, Integer, Long, Single, Double, Currency .

String

Date null Boolean #12:00:00 AM # Variant False) Empty (null Object


Nothing . object .

http://www.b4c4.com

:
.1 .2 .
.
"1# .
0
Itemname = Expression
Itemname .
Expression .
.1 "sum = 45+62#
.2 "name = "Amir"#
.3 "count = number#
.4 "x = 2 * n + i#
.5 "x = list1.listindex#

) (Private ) (Public ) .(Global


.
.
Dim Public Global
. . .


. 0
.1

...

http://www.b4c4.com

.2
.3

0
^
*
/
\
$
Mod
& $
0
2^3=8
2*3=6
6/2=3
7\2=3
2+3=5
2 3 = -1
11 Mod 3 = 2
Am + ir = Amir

or

Am & ir = Amir

http://www.b4c4.com

0
Not
And
Or
Xor
Eqv
Imp
0
Dim a, b, c as Boolean
c = True
a = Not b

a = True

a = b And c

a = False

a = b or c

a = True

a = b Xor c

a = True

a = b Eqv c

a = False

a = b Imp c

a = True

And

Or

Xor

Eqv

Imp

0
<
< 1
>

http://www.b4c4.com

>1
1
<>

0
Dim a as Boolean, b, c as Integer
b = 75
c = 21
a = True

a = b > c

a = True

a = b >= c

a = False

a = b < c

a = False

a = b <= c

a = False

a = b = c

a = True

a = b <> c

0
.1"#
.2 ^
.3
.4 * /
.5 \
Mod .6
.7 $

0
.1 * / $ .
.

http://www.b4c4.com

.2 .
.
0
X = 2 ^ 3 ^ 4
3 4 2 81 .
X = 4096
.3 . .

.4 . .

If

If .

Then If
Vb
End If

Then If

http://www.b4c4.com

1 Vb
Else
2 Vb
End If


Then If


Else 2 Then 1 If

.
1 2 .
Then .
1 2 .

0
End If .

0
Dim x as Boolean
If x Then
" Text1.Text = "The Value of x is True
Beep
End If

http://www.b4c4.com

x .
#
". Beep .

0
vb vb .
0

Dim x as Boolean
If x = True Then
" Text1.Text = "The Value of x is True
Beep
End If
0

Dim Res1 as Integer


If Res1 <= 0 Then
" Text1.Text = "The number is positive
Else
" Text1.Text = "The number is negative
End If
Res1
" "The number is positive " "The number is negative .
0
vb # " .
0

http://www.b4c4.com

"If x = Y Then Text1.Text = "Equal

x y Equal .
0
If a > b Then Max = a Else Max = b

a b a b .

ElseIf
If ElseIf .
.
0
ElseIf

If a > 0 Then
" Text1.Text = "positive
ElseIf a < 0 Then
" Text1.Text = "negative
Else
" Text1.Text = "zero
End If

.

http://www.b4c4.com

Select Case
ElseIf .
Select Case .
ElseIf Select Case .

Select Case
Case 1
1
Case 2
2
.
.
.

Case Else
Else
End Select

Select Case .
Case .

Select Case .
Else .
0

http://www.b4c4.com

Select Case sVal


Case 0
Text1.Text = "zero "
Case 1
Text1.Text = "One "
Case 2
Text1.Text = "Two "
Case 3
Text1.Text = "Three "
Case 4
Text1.Text = "Four "
Case 5
Text1.Text = "Five "
Case Else
Text1.Text = "Error "
End Select

. To Select Case
0

Case 10 To 20
. Is Select Case
0

Case Is > 50
. OR

http://www.b4c4.com

Case Is < 0, 10 To 20
10 20 Case .

IIF

)Function IIf(Expression, TruePart, FalsePart


.
TruePart FalsePart .
0

Dim IsZero as Boolean


Dim Mark as Integer
)IsZero = IIf(Mark = 20, True, False
20 IsZero
.


.
0For
.

http://www.b4c4.com

]For Counter = Start_Value To End_Value [Step Step_Count



]Next [Counter
Counter
Start_Value
End_Value
counter Start_Value Step_Count
Step_Count End_Value .
0
Step_Count .
Counter Next .
0
Dim i, sum as Integer
For i = 0 To 100
sum = sum + i
Next i
Text1.Text = sum
0 100 .

Dim i, sum as Integer


For i = 1 To 10 Step 2
sum = sum + i
Next i
Text1.Text = sum
1 10 .
Dim i as Single
Dim Counter as Integer

http://www.b4c4.com

For i = 5 To -5 Step -0.5


Counter = Counter + 1
Next
Text1.Text = Counter
5 -5 -0.5 .
For .
Exit For .
0
For i = 10 To 100
If (i Mod 7) = 0 Then Exit For
Next i
Text1.Text = "The first number is " & i
7 10 . 7 Exit For
.
0
For i = 0 To 100
If (i Mod 2) = 0 Then
Even = Even + 1
Else
Odd = Odd + 1
End If
Next i
Text1.Text = "Even numbers: " & Even & " Odd numbers: " & Odd
0 100 .

While
. While .

http://www.b4c4.com

0 .
While condition

Wend
0
While Not state
List1.AddItem i
i = i + 1
If i > 20 Then state = True
Wend
state . 0 20 .

Do

Do .
0
Do while condition

Loop
. .
0
Dim i, sum1 as Integer
Dim State as Boolean
State = True
Do While State
Sum1 = sum1 + i
i = i + 1

http://www.b4c4.com

If i > 100 Then State = False


Loop
Text1.Text = sum1
. 100 0

0
Do

Loop while condition

. .
0
Dim i, sum1 as Integer
Dim State as Boolean
Do
State = True
Sum1 = sum1 + i
i = i + 1
If i > 100 Then State = False
Loop While State
Text1.Text = sum1
0

Do Until condition

Loop

http://www.b4c4.com

.
.
0
Dim x as Integer
x = 1
Do Until x < 0
List1.AddItem x
x = x + 1
If x > 20 Then x = -1
Loop
1 20 .

Do

Loop Until condition

.
.
0
Dim x as Integer
x = 1
Do
List1.AddItem x

http://www.b4c4.com

x = x + 1
If x > 20 Then x = -1
Loop Until x < 0
1 20 .

Do Exit Do .
0
Dim x as Boolean
Dim ii as Integer
Do While 1
List1.AddItem ii
ii = ii + 1
If ii > 20 Then Exit Do
Loop
Exit Do . 0 20 .

0
.
.

.
. .
For Next
Next.
0

http://www.b4c4.com

For i = 0 To 10
For j = 0 To 10
For k = 0 To 10

Next k, j, i

For i = 0 To 10
For j = 0 To 10
For k = 0 To 10

Next k
Next j
Next i


.
0
.1 .
.2 . .
.3 # "...

]Dim ArrayName(LowerValue To HigherValue) [As DataType

http://www.b4c4.com

Dim Private
Public .
Dim .

ArrayName .
LowerValue HigherValue .
. .

Dim sTestArray(0 To 10) as String


Dim sTestArray(10) as String

# Option Explicit
" .
Option Base 1
.

ArrayName(Index) = Value
0
)iArray(3) = iArray(7

7 3 .

0
.

http://www.b4c4.com

.
LBound UBound #. "
0

0

.

)UBound(ArrayName, Dimension
'ArrayName = the name of the array
)LBound(ArrayName, Dimension
0
Comment Rem .
.
Dimension .
0
Dim Names(100) as String
LowerVal = LBound(Names) 'Get the lower boundary number.
UpperVal = UBound(Names) 'Get the upper boundary number.

For i = LowerVal To UpperVal


List1.AddItem Names (i) 'Add each name from array according
'to how many stored in the Array
Next
:
. Microsoft Excel .
0

http://www.b4c4.com

Static iArray(1 To 2, 1 To 3) as Integer


0

)iArray(1,1), iArray(1,2), iArray(1,3


)iArray(2,1), iArray(2,2), iArray(2,3

2 3 .
3 Debug .
# 2 " .
.
0
Option Explicit
)(Private Sub Form_Load
Dim i, j as Integer
Dim TableArray(1 To 10, 1 To 10) as Integer

For i = 1 To 10
For j = 1 To 10
TableArray(i, j) = i * j
Text1.Text = Text1.Text & TableArray(i, j) & vbTab
Next j
Text1.Text = Text1.Text & vbCrLf
Next i
End Sub
.
Properties
MultiLine True.

http://www.b4c4.com

vbTab 8 .
vbCrLf .
.

0
.
0
Dim MyArray (10000) as Long
40004 10001 * 4 . Long
.
10 4000400
.
. RunTime
ReDim .
. .
.

Dim ArrayName() as DataType


ReDim .

)ReDim ArrayName(LowerValue To HigherValue


0
ReDim .
0

http://www.b4c4.com

Dim Names() as String

)(Private Sub Form1_Load


)ReDim Names(1 To 10
End Sub
. .
0
ReDim # " .
. ReDim Preserve .
)ReDim Preserve ArrayName(LowerValue To HigherValue


.
Const 0
[Private/Public] Const ConstantName [As DataType] = Value
.
0
Const Pi = 3.14
"Private Const WelMsg as String = "Hi
Public Const T_1 = &HF
0
.1 Const Private .
.2 As DataType Variant .

http://www.b4c4.com

.3 "Public Const# .
&H .4 . 16 .

. Declaration
#. Option Explicit !"
. .
. .
Static Static .
.

Static .

! . Static .
0
]Static StaticVarName [As DataType



. .
.
0
.1 .
.2 .
.3 .
.4 .

http://www.b4c4.com

.5 .

0
.1 "Sub#
.2 "Function#

.
.
.

0
]]) [As DtataType [([Private/Public] Function FuncName
vb
End Function
0
.1 .
[As DataType] .2 . Variant
.
.3 .
.4 # "
.5 .
= FuncName
.6 0

http://www.b4c4.com

# " 1

.
.7 .

0
Private Function Fact(n as Integer) as Long
.
.
.
End Function
Fact . Long
.
n Fact .

)(Public Function GetComputerName


.
.
.
End Function
GetComputerName .

Function Arr(x1 as Integer, x2 as String, status as Boolean) as Boolean


.
.
.
End Function

http://www.b4c4.com

. .

0
Dim Ret1 as Long
)Ret1 = Fact (5

Dim Name1 as String


Name1 = GetComputerName

Dim x as Boolean
)x = Arr(m, "Amir", False

! vb .

)] [([Private/Public] Sub SubName


vb
End Sub
0
.1 .
.2 .
.3 .

http://www.b4c4.com

.4
" 0

)(Sub Show_results
.
.
.
End Sub
0
Show_results

)Sub GetInfo1(Name as String, StNo as Integer
.
.
.
End Sub
0
GetInfo1 "Amir", 2546
.

" 0Call
call

Call Show_results
)Call GetInfo1("Amir", 2546
call .

http://www.b4c4.com

.5 .

0
0
" " ByVal #
" " ByRef #


.
.

. .

0
)Sub Pro(ByVal a as Integer, ByRef b as Integer
.
.
.
End Sub
a b .

vb .
.
Optional .
0

http://www.b4c4.com

)Sub Pro(Optional a as Boolean


.
.
.
End Sub
0
)Call Pro(True

Call Pro
a False .


.
0
)Sub Pro1(Optional num as Integer = 5
.
.
.
End Sub
0
.
0
.1
# "
0

http://www.b4c4.com

)Function Fact(n as Integer


.
.
.
)Fact = Fact(n - 1
.
.
.
End Function
.2
a b b a .
...
0
)Function Proc1 (m as Integer
.
.
)Proc1 = Proc2 (m
.
.
End Function
)Function Proc2 (n as Integer
.
.
)Proc2 = Proc1 (n-1
.
.
End Function
...

http://www.b4c4.com

()Object Oriented Programming


. ...
.
.
. . #"
. . .
vb "Form # .

vb

. # TextBox "
.

vb
vb visual studio visual studio . visual c
visual dBase visual InterDev .
vb Start Menu Microsoft visual studio 6.0 . vb
Microsoft visual basic 6.0 . vb
New Project . .
Standard EXE Standard EXE .
. Existing
Recent .

http://www.b4c4.com

New Standard EXE Open . Design


.
. Project
Explorer .
.

Project Explorer

0
0 View Code .
# . ".
0 View Object .
0 Toggle Folders Project Explorer
# . ".
Project Explorer . Add
Form .

Project Explorer Properties .


. .
Form Layout .
. Drag & Drop .
Startup Position .
Manual

Drag & Drop

.
Center Owner .

vb

.
Center Screen .
Windows Default .

http://www.b4c4.com

Toolbox #. "
.
.

: PictureBox
.
0 Name .
0 Align # . "...
0 Appearance .
0 AutoSize True

PictureBox

0 Backcolor .

0 Height .

0 Left .

0 Picture .
0 ToolTipText
pop-up .

0 Top .

0 Visible False .

0 Width .

: Label
.
0 BackStyle # Transparent ."

# Opaque"

0 Caption .

0 Font "Font # .

0 ForeColor

http://www.b4c4.com

0 MouseIcon .
MousePointer 99-Custom .

: TextBox
# . "
0 Locked True .
0 MaxLength

5 .
0 MultiLine True Enter
False .
0 PasswordChar
#. "Password #
"
0 ScrollBars

Scroll Bars .

0 0 0 .

0 Text .

: Frame
.
.

: CommandButton

http://www.b4c4.com

. .
.
0 Cancel True

Esc

. .
0 Caption .
0 Default True
Enter .
.
0 Enabled False # " .

: CheckBox
. .
0 Value . Unchecked 0 Checked 0
0

Grayed . Grayed

. Setup
Grayed .

: OptionButton
.
Frame .

0 Value True False .

: ComboBox
.

http://www.b4c4.com

0 Sorted True Combo Box


.
0 Text Combo Box .

: ListBox
.
0 List .

0 MultiSelect 0 . Simple
.

Extended

Ctrl . Shift
.
0 Style 1 CheckBox .

( HScrollBar) ( VScrollBar) :
.
#. "
LargeChange 0 SmallChange

Min 0 Max ScrollBar .

: Timer
Timer .
0 Enabled True Timer
.

http://www.b4c4.com

0 Interval Timer .

1000

: DriveListBox
.

: DirListBox
.

: FileListBox
.
0 Pattern . * *.
.
0 Normal,System,ReadOnly .

True .
: Shape
.

0 BorderStyle # . "...

0 BorderWidth .
0 Shape 00 #. 01 02 03 04 05
"

: Line
.

0X1 x

0X2 x

0Y1 y

0Y2 y

http://www.b4c4.com

: Image
.
0 Stretch True Image Resize
.

: Data
vb .
.

): OlE(Object Linking and Embedding


#
" . OLE Babylon . OLE
.

vb
vb vb ! Hello world .
vb New Project Standard EXE open .
Label Command Button .
CmdOk CmdCancel #

Properties " .

http://www.b4c4.com

Caption &Ok &Cancel & .


Alt
.
. Command Button .
Command Button .
.
)(Private Sub CmdOk_Click

End Sub
Sub
Underline .
vb . .
Combo Box # Combo Box!"Combo .
Object Combo
.
. .

Ok

Cancel .
)(Private Sub CmdOk_Click
"!Label1.Caption = "Hello World
End Sub

)(Private Sub CmdCancel_Click


End
End Sub
vb #
Name "Caption #" .

http://www.b4c4.com

.
.
Space vb . .
Ok ! Hello World .
End #. ".
# " vb .
Start .
F5 .
) (EXE Make Project1.exe .
Ok.

: MsgBox
!
vb MsgBox .

Function MsgBox(Prompt, [Buttons as VbMsgBoxStyle = vbOKOnly], [Title],


[HelpFile], [Context]) as VbMsgBoxResult
.
0Prompt .
0Buttons Ok .
0 VbMsgBoxStyle

Const vbAbortRetryIgnore = 2
Const vbApplicationModal = 0
)Const vbCritical = 16 (&H10
Const vbDefaultButton1 = 0

http://www.b4c4.com

Const vbDefaultButton2 = 256 (&H100)


Const vbDefaultButton3 = 512 (&H200)
Const vbDefaultButton4 = 768 (&H300)
Const vbExclamation = 48 (&H30)
Const vbInformation = 64 (&H40)
Const vbMsgBoxHelpButton = 16384 (&H4000)
Const vbMsgBoxRight = 524288 (&H80000)
Const vbMsgBoxRtlReading = 1048576 (&H100000)
Const vbMsgBoxSetForeground = 65536 (&H10000)
Const vbOKCancel = 1
Const vbOKOnly = 0
Const vbQuestion = 32 (&H20)
Const vbRetryCancel = 5
Const vbSystemModal = 4096 (&H1000)
Const vbYesNo = 4
Const vbYesNoCancel = 3

. 0Title
. 0HelpFile
. 0Context
0
Private Sub Form_Load()
MsgBox "Welcome to vb world!", vbInformation, "First visit"
End Sub
. MsgBox
0
Private Sub Form_Unload(Cancel as Integer)
Cancel = 1

http://www.b4c4.com

Dim Ret1 as VbMsgBoxResult


Ret1 = MsgBox("Are you sure you want to exit this program?", vbCritical +
vbYesNo, "Exit")
If Ret1 = vbYes Then
End
End If
End Sub
Close . vb
.
0
Private Sub Form_Unload(Cancel as Integer)
Cancel = 1
End Sub
0 MsgBox VbMsgBoxResult
Const vbOK = 1
Const vbCancel = 2
Const vbAbort = 3
Const vbRetry = 4
Const vbIgnore = 5
Const vbYes = 6
Const vbNo = 7
. Unload Form_Unload

: InputBox
.
Function

InputBox(Prompt,

[Context]) as String

[Title],

[Default],

[XPos],

[YPos],

[HelpFile],

http://www.b4c4.com

0 Prompt .
0Title .
0Default .
0XPos X .
0YPos Y .
0HelpFile .
0Context .
String .
0
)(Private Sub Form_Load
Dim Ret1 as String
Ret1 = InputBox("What's your name?", "Please confirm your name", "Amir", 0,
)0
If Ret1 <> "" Then
Me.Caption = "Welcome" & " " & Ret1
Else
"Me.Caption = "Unknown user
End If
End Sub
.
Cancel Unknown user .
Me vb .
Form1 .
0
0 vb

Me

http://www.b4c4.com

vb . .
. Timer
.
Label AutoSize True
Timer Interval 1000 .
)(Private Sub Timer1_Timer
Label1.Caption = Time
End Sub
Now Time .
)(Private Sub Timer1_Timer
Label1.Caption = Now
End Sub
0
0
)(Private Sub Form_Load
Dim i as Integer
For i = 0 To 255
)List1.AddItem i & " : " & Chr$(i
Next i
End Sub
ListBox .
)(:Chr$
Function Chr$(CharCode as Long) as String
CharCode .
)(:Asc
Function Asc(String As String) As Integer
ANSI .

http://www.b4c4.com

)(Private Sub Form_Load


)"MsgBox Asc("A
End Sub
)( LCase$ )(: UCase$
Function LCase$(String as String) as String
Function UCase$(String as String) as String
LCase$ UCase$ .
0
TextBox CommandButton .
)(Private Sub Command1_Click
)Text1.Text = LCase$(Text1.Text
End Sub
)(Private Sub Command2_Click
)Text1.Text = UCase$(Text1.Text
End Sub
0
.
vb # Form1 " . .
Project Explorer Project Explorer #.
" Add Form .
Form Open . Form2 .

Project

Explorer Form1 . TextBox


CommandButton . Caption Ok
Cancel 1.

http://www.b4c4.com

)(Private Sub Command1_Click


'For Ok button
If LCase(Text1.Text) = "password" Then
Unload Me
Form2.Show
Else
MsgBox "Invalid Password!", vbCritical
Text1.SetFocus
"}SendKeys "{Home}+{End
End If
End Sub
)(Private Sub Command2_Click
'For cancel button

End

End Sub
0
Unload Me 1 # ".
0 Form2.Show Show Form2 .
0

0 Text1.SetFocus SetFocus .
"}: SendKeys "{Home}+{End
SendKeys vb .
.
SendKeys 0
^ = Control
{enter} = Enter
% = Alt

http://www.b4c4.com

{Del} = Delete
{ESCAPE} = Escape
{TAB} = Tab
+ = Shift
{BACKSPACE} = Backspace
{BREAK} = Break
{CAPSLOCK} = Caps Lock
{CLEAR} = Clear
{DELETE} = Delete
{DOWN} = Down Arrow
{LEFT} = Left Arrow
{RIGHT} = Right Arrow
{UP} = Up Arrow
{NUMLOCK} = Num Lock
{PGDN} = Page Down
{PGUP} = Page Up
{SCROLLLOCK} = Scroll Lock
{F1} = F1 .......Use {F2} {F3} and so On For others...
{HOME} = home
{INSERT} = Insert Upload
0
Private Sub Command1_Click()
SendKeys "%{F4}"
End Sub
"Alt+F4 #.
Private Sub Command1_Click()
Dim i as Integer 'declare the variable

http://www.b4c4.com

For i = 1 To 150 'how many times (you can change the 150 to whatever you
want)
SendKeys "{CAPSLOCK}", True 'turn on the capslocks light, Then turn it
off
SendKeys "{DOWN}", True 'just To give more time
SendKeys "{DOWN}", True 'just To give more time
SendKeys "{SCROLLLOCK}", True 'turn on the scroll lock light, turn it off
SendKeys "{DOWN}", True 'give more time
SendKeys "{DOWN}", True 'give more time
Next i
End Sub
.


.
vb .
.
0vb ( Mode)
Input

.1

Output

.2

Append

.3

Random

.4

.5

http://www.b4c4.com

Binary

Input .

Output .
Append . Output .
Output . Output
. Random .
Binary .
0
.
"
. ) .(Loc
".
0
vb Open 0

Open StrFileName For fMode as #intFileNumber

StrFileName .
0

"StrFileName = "C:\Test1.txt
"StrFileName = "D:\Files\newTest.dat
fMode )(Input,Output
intFileNumber . vb
1# "255 . .
0

http://www.b4c4.com

. Close .

]Close [intFileNumber1, intFileNumber2,

Close .
Close
0
Close 1, 3
1 3 .
Open "C:\test.txt" For Output as #1
Close 1
C: . .
!
0
.1

0 Print
Print #filenumber, expression

Filenumber .
0
# " number sign "##.
Expression .

Open "c:\test.txt" For Output as #1


"Print #1, "Amir
Close 1
Amir " "d:\test.txt .
...

http://www.b4c4.com

. vb .

.2

0 Write
Write #filenumber, expression

Write Print .
Write .
. .

0
Line Input #filenumber, variablelist

filenumber # Input "


Variablelist #.
"
0
)(Private Sub Form_Load
Dim fData as String
Open "c:\test.txt" For Input as #1
Line Input #1, fData
Close 1
MsgBox fData
End Sub

" "c:\test.txt fData


.

(Run-Time

) Error . .

http://www.b4c4.com

line . .
0
!Hello,World
?Are you there
Input Line 0

Input #1, fData


Hello fData .
. . vb
.

Function EOF(FileNumber as Integer) as Boolean


.
.
0
0NotePad
# NotePad open
".
Standard EXE .
MultiLine True ScrollBars 3 # "
Open 0

)(Private Sub mnuOpen_Click


on Error GoTo Err_Not_Found

Dim FileName, Temp, Total as String

http://www.b4c4.com

Dim FileNumber as Integer


_ FileName = InputBox("Enter the path for a text file",
)"Open", "C:\test.txt", 0, 0
If FileName = "" Then Exit Sub
FileNumber = FreeFile

Open FileName For Input as FileNumber


)While Not EOF(FileNumber
Line Input #FileNumber, Temp
)Total = Total + Temp + Chr(10) + Chr(13
Wend
Close FileNumber
Text1.Text = Total
Exit Sub
Err_Not_Found:
MsgBox Err.Description & vbCrLf & Err.Number
End Sub
FileName .
FreeFile .
Open
Temp .

Total

Chr(10) +

) Chr(13 . Carriage-return/Linefeed
vb .
vbCrLf
vb . On Error .

Crash .!

http://www.b4c4.com

On Error Resume Next


...

On Error GoTo label


label 0
)(Private Sub Form_Load
On Error GoTo label

label:

End Sub

:Err
.
.
# " .

)(Private Sub mnuSave_Click


on Error GoTo Err_Found
Dim FileName as String
Dim FileNumber as Integer
_ FileName = InputBox("Enter the path for a text file to save",
)"Open", "C:\test.txt", 0, 0
If FileName = "" Then Exit Sub
FileNumber = FreeFile

Open FileName For Output as FileNumber

http://www.b4c4.com

Print #FileNumber, Text1.Text


Close FileNumber
Exit Sub

Err_Found:
MsgBox Err.Description & vbCrLf & Err.Number
End Sub

:Loc
Function Loc(FileNumber As Integer) As Long
. " # Read/Write Locate
. Binary

:Input
Function Input$(Number As Long, FileNumber As Integer) As String
. Number
LOF . Line Input
0 .

Open FileName For Input As FileNumber


Text1.Text = Input$(LOF(FileNumber), FileNumber)
Close FileNumber

:LOF

Function LOF(FileNumber As Integer) As Long

http://www.b4c4.com

) (Length Of File .

0
.
Binary .
.
.

Open FileName For Binary [Access Read/Write] As #intFileNumber

)(Private Sub mnuOpen_Click


On Error GoTo Err_Found
Dim Temp As String
Dim FileNumber As Integer

_ FileName = InputBox("Enter the path for a text file to open",


)"Open", "C:\test.txt", 0, 0
If FileName = "" Then Exit Sub
FileNumber = FreeFile

Open FileName For Binary As FileNumber


))Temp = Space$(LOF(FileNumber
Get #FileNumber, , Temp
Close FileNumber
Text1.Text = Temp
Exit Sub

http://www.b4c4.com

Err_Found:
MsgBox Err.Description & vbCrLf & Err.Number
End Sub

.
Space .

Function Space$(Number As Long) As String

Get .

Put .
Put #FileNumber, , Expression

:Seek

Function Seek(FileNumber As Integer) As Long

:FileLen

Function FileLen(PathName As String) As Long

http://www.b4c4.com

))Temp = Space$(FileLen(FileName

) (Delete .

:Kill

)Sub Kill(PathName

Delete .0

Kill C:\test.txt

:FileCopy

)Sub FileCopy(Source As String, Destination As String

FileCopy C:\test.txt , D:\New\1.txt

0
.

http://www.b4c4.com

.
. .
0
0
.
.

Public Function FileExists(filename As String) As Boolean


FileExists = (Dir(filename, vbNormal Or vbReadOnly Or vbHidden Or
vbSystem Or vbArchive) <> "")
End Function

Function FileExists(filename$) As Boolean


Dim f%
' Trap any errors that may occur
On Error Resume Next
' Get a free file handle to avoid using a file handle already in use
f% = FreeFile
' Open the file for reading
Open filename$ For Input As #f%
' Close it
Close #f%
' If there was an error, Err will be <> 0. In that case, we return False
FileExists = Not (Err <> 0)
End Function

http://www.b4c4.com

Public Function FileExists(TheFileName As String) As Boolean


'Sees if the string(file) you specified exists
If Len(TheFileName$) = 0 Then
FileExists = False
Exit Function
End If
If Len(Dir$(TheFileName$)) Then
FileExists = True
Else
FileExists = False
End If
End Function

Function fileexists(ByVal sFileName as String) as Boolean


Dim TheFileLength as Integer
on Error Resume Next
TheFileLength = Len(Dir$(sFileName))
If Err or TheFileLength = 0 Then
fileexists = False
Else
fileexists = True
End If
End Function

.

http://www.b4c4.com

)Windows API (Application Programming Interface


. ) (VB,C++,
.

) (.EXE

. .
.dll Link # "
# dll " .
# . dll
". Setup
. ...

dll

dll .
dll Dynamic Link Library .
.

xp

\WINDOWS\SYSTEM32 \WINDOWS\SYSTEM .
.

User32.dll
... .

GDI32.dll
API
.

Kernel32.dll
.

.0

Advapi32.dll
API Registry .Security

Comdlg32.dll
API Common dialog
Common dialog .

Lz32.dll
32

Mpr.dll
API Multiple Provider Router

Netapi32.dll
32

Shell32.dll
32 Shell API

Version.dll
Version

http://www.b4c4.com

http://www.b4c4.com

Winmm.dll

Winspool.drv
Print Spooler Interface

API . c++ API


. .

ATOM

ByVal variable as Integer

BOOL

ByVal variable as Long

BYTE

ByVal variable as Byte

CHAR

ByVal variable as Byte

COLORREF

ByVal variable as Long

DWORD

ByVal variable as Long

HWND

ByVal variable as Long

HDC
HMENU
INT

ByVal variable as Long


ByVal variable as Long
ByVal variable as Long

UINT

ByVal variable as Long

LONG

ByVal variable as Long

LPARAM

ByVal variable as Long

LPDWORD

variable as Long

LPINT

variable as Long

LPUINT

variable as Long

LPRECT

variable as Type any variable of that User Type

LPSTR

ByVal variable as String

http://www.b4c4.com

ByVal variable as String

LPCSTR

variable As Any use ByVal when passing a string

LPVOID

variable as Integer

LPWORD

ByVal variable as Long

LPRESULT

ByVal Nothing or ByVal 0& or vbNullString

NULL

ByVal variable as Integer

SHORT

Sub Procecure not applicable

VOID

ByVal variable as Integer

WORD

ByVal variable as Long

WPARAM

API 0

Version Compatibility
Update
. Crash .
File Size
API .
.

(Load

) Time . . Setup
Setup
. .
Documentation
dll .
dll .
.

http://www.b4c4.com

API .
API vb . vb
dll .

dll : vb
dll vb Declare Dll . ) (Declaration
# "

][Private/Public] Declare Function/Sub Ex_name Lib Dll name [Alias In_name


(Parameters list & Type) As DataType

Private Public . .
Function/Sub Ex_name . .

Dll name

dll .
Alias .
.0

"Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx


(ByVal uFlags As Long, ByVal dwReserved As Long) As Long


vb .
...
vb Add-Ins .
Viewer API .

API

http://www.b4c4.com

vb Add-Ins Add-In Manager .


. VB 6 API Viewer Load On Startup
.
Ok . Add-Ins API Viewer .

Load Text File

WIN32API.txt . Syntax
vb .

API . 0
Declare
.1 Declare Public #"
.
.2 Declare Private .

ALIAS
.3 API

ALIAS A .

W=UNICODE
.4 NT W .
.5 dll vb ALIAS .

Variable Type
.6 dll Variant .
ByRef .7 vb .
AS ANY .8 .

A=ANSI ,

http://www.b4c4.com

Strings
API .9 .
.10 ByVal .
.11 ByRef
.
.12 vb ByVal API .
.13 vb 255 .

Arrays
.14 API ByRef .

Callback Function
.15 AddressOf .
.16 Procedures .bas.

Passing a null value


.17 null )( .
.18 vbNullString vb .
.19 null Long & 0 .
Window Handle
.20 Handle .
.21 vb Handle hWnd .
Handles .22 Long .

Callbacks
.23 API . Callbacks .

http://www.b4c4.com

vb .24 callbacks AddressOf API .


.25 Callback .

Subclassing
.26 .
.27 .
.28 SetWindowsLong .

Miscellaneous
.29 ByVal .
Handles .30 ByVal Long .

30 . API .

API
.

Wave :vb
vb . .

Option Explicit
'Declares
"Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA
(ByVal lpszSoundName as String, ByVal uFlags as Long) as Long
Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () as Long
'Constants

http://www.b4c4.com

Const SND_SYNC = &H0 'just after the sound is ended exit function
Const SND_ASYNC = &H1 'just after the beginning of the sound exit function
Const SND_NODEFAULT = &H2 'if the sound cannot be found no error message
Const SND_LOOP = &H8 'repeat the sound until the function is called again
Const SND_NOSTOP = &H10 'if currently a sound is played the function will
return without playing the selected sound
Const Flags& = SND_ASYNC or SND_NODEFAULT

Private Sub Command1_Click()


Dim i as Long
Const SoundFileName as String = "D:\Music\Persian\MARYAM\1.wav"

i = waveOutGetNumDevs()
If i > 0 Then 'There is at least one sound device.
i& = sndPlaySound(SoundFileName, Flags&)
Else
MsgBox "You don't have a sound card to play sounds!", vbCritical, "Error"
End If
End Sub
. waveOutGetNumDevs
. SoundFileName

sndPlaySound

.
.

http://www.b4c4.com

Private

Declare

Function

GetWindowsDirectory

Lib

"kernel32"

Alias

"GetWindowsDirectoryA" (ByVal lpBuffer as String, ByVal nSize as Long) as Long

Private Function Winfolder() as String


Dim strsave as String
strsave = String(255, Chr$(0))
Winfolder = Left$(strsave, GetWindowsDirectory(strsave, Len(strsave)))
End Function

Private Sub Form_Load()


MsgBox Winfolder
End Sub

You might also like