Write Your First Visual Basic Program: Lesson 1
Write Your First Visual Basic Program: Lesson 1
Lesson 1
What Is Visual Basic and Why do I need it?
Visual Basic is Easy to learn Programming language.
With Visual Basic you can develop Windows based applications and games.
Visual Basic is much more easier to learn than other language (like Visual C++),
and yet it's powerful programming language.
Visual Basic suit more for application developing than for Games developing.
You can create sophisticated games using Visual Basic, But
If you want to make a really advanced professional game like Quake 2,
You may choose other language (like C++), that would be much more
harder to program with.
However, Visual Basic will be probably powerful enough to suit all your application
and games programming needs.
Getting Started
Note that all the images in this tutorial taken from Visual Basic version 6.0.
If you using other version of Visual Basic, the images that you will see may
be a little different.
Run the Visual Basic program. The first thing you see is:
Figure 1
Here you choose what is the kind of the program you want to create.
For creating a simple Visual Basic program, choose the Standard EXE,
and press the Open Button.
(If Figure 1 is not the first thing you see when you run Visual Basic,
choose from the Visual Basic menu File->New Project (Figure 2))
Figure 2
After you've clicked the Open button, you will see:
Figure 3
Look at the form with the title bar Form1.
This is how your program will look like.
Everything you will place on this form will appear in your program.
As you can see, your form is currently empty.
You didn't program anything yet, but lets run the program!
Figure 4
Figure 5
To stop the program from running, you can click on the Stop button
in the Toolbar (Figure 6), or click the form's X button (Figure 7).
It's very recommended that you will always close your programs using the
form's X button instead of the Visual Basic Stop button.
It's like shutting Windows From the Start button, instead of Pressing
the Power button.
Figure 6
Figure 7
Back Forward
To see the form properties, select from the menu View->Properties Window (Figure
8).
Figure 8
The properties window looks like this:
Figure 9
In the red circle you can see the component name:
These are the properties of a Form, and the form's name is Form1.
The column marked by the blue circle contains the form's properties names:
The form has Name property, Appearance property AutoRedraw property, and more.
The column marked by the black circle contains the form's properties values:
The form's Name is Form1, The form's Appearance property is 1 - 3D, and so on.
To change the Caption property, simply click on the Caption property in the
form's properties names column (Figure 10).
Figure 10
Figure 11
Now run the program using the Play button (Figure 4).
You will see that the text on the form's Title bar is Hello (Figure 12).
Figure 12
Figure 13
And you will see the Toolbox (Figure 14):
Figure 14
Now lets add a button to your form.
Buttons in Visual Basic called "Command Buttons".
To add a button, click on the Command Button icon in the Toolbox (Figure 15).
Figure 15
As results, the Command Button icon will look pressed (Figure 16).
Figure 16
Now click on the form with the left mouse button, and hold the button
down while moving the cursor.
You will see a rectangle.
Release the mouse button, and in the place of the rectangle you
will see a button (Figure 17).
Figure 17
Figure 18
The Command Button's Properties window (Figure 19):
Figure 19
As you can see, In the top of the properties window, right under the title bar,
appears "Command1 CommandButton".
Command1 - because it's the name of this specific Command Button (look
at the name property).
Change the Command Button's BackColor property to blue (or other color).
The change won't take affect, untill you will set the Command Button's Style
property to 1 -Graphical.
You can now play a little bit with the Command Button's properties,
this is the best way to learn what every property does.
You can also add other controls from the Toolbox to your form,
And play around with their properties.
Figure 20
MsgBox "Hello"
Insert the line MsgBox "Hello" to the Form_Load event (Figure 21).
Figure 21
Figure 22
More Events
The Form has more events besides the Form_Load event.
How can you find them?
Click on the Drop-Down List that found in the upper left corner of
the Code Window, where appears right now the text "Form" (Figure 23).
Figure 23
You will see a list of the components that found in your program.
You have 1 command button with the name "Command1" and 1 Form.
Here you select which component's event you want to program.
We want to program a form's event, so select "Form" from the list (Figure 23).
Figure 24
You will see the complete list of the form's events:
Load, LostFocus, MouseDown, MouseMove and more.
MsgBox "GoodBye"
After you inserted this line to the Click event the Click event should look like this:
As you can see, the Command Button's Click event called "Command1_Click",
because the name of the specific Command Button that we program
its Click event is "Command1".
If we had program the Click event of a Command Button with the
name "BlahBlah7", the Click event would be called "BlahBlah7_Click".
Every component has its own unique events, therefore if you had
5 Command Buttons on your form, every one of them has its
own unique Click event.
Print "Hello"
Figure 25
Add another Command Button to your form.
The New Command Button's name is "Command2" by default (Figure 26)
Figure 26
After you've done so, your code should look like this:
Private Sub Command1_Click()
Print "This is Command1"
End Sub
Lesson 2
Learning about Variables
Using Variables is one of the most basic and important subjects
in programming, so this lesson is very important.
You can store in Integer variable the number 0 or the number 375 or
the number -46, but you can't store the number 4.5 (Because of the .5)
or the number 100,000 (Because It's bigger than 32767) or the
number -50,042 (Because it's smaller than -32,768)
Dim = Declare
MyTest = the name of the new variable
As Integer = The new variable type will be Integer.
MyTest = 10
The line above will insert the number 10 into the MyTest variable.
Now the MyTest variable stores the Number 10, but how
can you access this value from your program?
You can do that using the variable name. Example:
Print MyTest
Print MyTest
and
Print "MyTest"
Where you putting a text inside quotes (Like in the bottom line),
Visual Basic treat it as a Text, and will print it As-Is.
The Print "MyTest" Line will print MyTest on the form.
Where you putting a text Without quotes (Like in the upper line),
Visual Basic treat it as a variable name, and will print the value that found
in the variable.
The Print MyTest Line will print the value that found in
the MyTest variable, therefore it will print 10 on the form.
Answer:
0
20
Blah
30
Why is that?
Lets pass over the code line after line:
Blah = 10
Now the Blah variable holds the number 10
Blah = 20
Now the Blah variable holds the number 20
What's happened to the 10 that was inside it?
It has been deleted!
A variable can holds only one value, and when
you put in it a value, the old value is being immediately deleted.
Blah = 20
and
Blah = 10
Blah = 20
There is no differents!
In both cases the Blah variable will hold the number 20
Print Blah
Will print the value that found right now
in the Blah variable - 20
Blah = 30
Now the Blah variable holds the number 30
Print "Blah"
Will print the Text that found between the quotes - Blah
Print Blah
Will print the value that found right now
in the Blah variable – 30
Answer:
5
2+3
5
2
3
6
Why is that?
Lets pass over the code line after line:
Blah = 2
Now Blah holds the value 2
Print 2 + 3
When you execute command (the Print command in the case)
on expression, Visual Basic will evaluate the expression first,
and then will execute the command on the evaluation result.
Print Blah
Will replace the Blah with its value.
Because the Blah value is 2, After the replacement
the new command will be Print 2
After executing this command, the value 2
will be printed on the form.
Blah = Blah + 1
The line above simply says:
Put in the Blah variable, the value of the expression Blah + 1
The computer is first evaluate the expression Blah + 1
Blah is being replaced with its value: 2.
After the replacement the computer evaluates the expression 2 + 1.
The expression value is 3.
So now, after the "Blah + 1" expression evaluation,
the command is: Blah = 3
As you know by now, this command will
put the value 3 in the Blah variable.
Summary:
After executing the command Blah = Blah + 1,
the value 3 will be inserted into the Blah variable.
Print Blah
Will replace the Blah variable with its value: 3.
So the command that will be eventually executed is Print 3
Print Blah
Will replace the Blah variable with its value: 6.
So the command that will be eventually executed is Print 6
For example:
2 + 3 * 4 is equal to 14
(2 + 3) * 4 is equal to 20
Answer:
The Integer variable can't store a number with fraction,
So the computer will round the 4.8 to 5, and will
Insert 5 to the ABC variable.
So the Command Print ABC will print 5 on the form.
The Answer is: Because the Byte variable is taking less memory.
And so on.
For example:
Answer:
kuku
Hello!!!
Why is that? Lets pass over the code line after line:
kuku = "Hello!!!"
Now the kuku variable holds the text Hello!!!
Print "kuku"
Everything that found inside quotes is being treated as text String,
So it will print kuku on the form.
The computer Is NOT associate the text "kuku" with
the variable kuku, because of the quotes.
Print kuku
Will replace the kuku with its value (Hello!!!),
and after the replacement will execute the new command Print "Hello!!!"
You can join two Strings together using the "+" operator.
Example:
The code below will print Hello !!! world on the form.
The expression gogo + " !!! " + popo is equal to
Hello + " !!! " + world and that's equal to "Hello !!! world"
So eventually, the command that will be executed is gogo = "Hello !!! world"
Scope of Variables
Insert two Command Buttons to your form (with
the names Command1 and Command2),
and Add the following code to your program:
For Example:
Figure 1
After you've done so, your code should look like this:
The Rules:
4)You can't call your variable with a name that is a Visual Basic
Command or Function (these names are been called "saved names").
For example you can't use the name Print, because there is
command in Visual Basic that called Print (which we used pretty much
in this tutorial).
You can know easily if a name is a saved name by typing it anywhere
in your code. All the saved names are being painted with blue color.
Few examples for saved names: Print, Sub, End, MsgBox
Lesson 3
The Command Button's KeyPress, KeyDown and KeyUp Events
The events that will be mentioned in the following pages
are commonly used, and without learning about them
you really can't go anywhere in Visual Basic programming.
Run the program, and click the button with the mouse.
Nothing has happened.
It's because the KeyDown, Key_Press, and KeyUp events are
being executed Only when you press a key on the keyboard.
Now press any key on the keyboard, hold it down for few seconds,
and then release it.
Your form will look like this:
Figure 1
Lets see:
The first event that been executed is the KeyDown event,
because "KeyDown" was the first text that been printed on the form.
When you release the key, the KeyUp event is being executed once.
Print Asc("b")
Will print on the form the Ascii value of the character "b".
Print Chr(98)
Will print on the form the character that its Ascii value is 98.
Every Sub can get no parameters (like the Click event), can get
one parameter (like the KeyPress event) or can get more than
one parameter.
These keys don't have Ascii value, but they have KeyCode value.
For example, the KeyCode value of the Ctrl Key is 17.
Ascii represent characters, while KeyCode represent Keyboard's keys.
Because of that, the characters "a" and "A" have different Ascii value,
but they have the same KeyCode value, because the same key
is typing "a" and "A".
If the user has pressed the Alt key and the Shift key together,
the Shift value will be 4 (for the Alt key) + 1 (for the Shift key) = 5
Examples:
The KeyUp event's parameters are the same as the KeyDown event's parameters.
Click on one of the mouse buttons when the mouse is over the button,
and hold the button down.
The MouseDown event is being executed.
Release the button, and the MouseUp event is being executed.
Print X, Y
Figure 2
Figure 3
Figure 4
BackColor - This is the background color of the button (Figure 5).
This property will take affect only after you will set the
Button's Style property to 1 - Graphical
Figure 5
Figure 6
DisabledPicture - The Picture that will appear on the button when
it disabled (When the Enabled property is "False", like in Figure 4).
MousePointer - Choose here the mouse pointer (Arrow, Hourglass, and more)
when it will be over the button.
If you will set the property to be 99-Custom,
The mouse pointer will be the icon that you will select in the MouseIcon property.
Icon - The Icon that appear in the form's title bar (Figure 7)
and in the Task bar (Figure 8).
Figure 7
Figure 8
ControlBox - Set this property to "False" to remove
the title bar's Close, Minimize and Maximize buttons (Figure 9).
Figure 9
TheControlName.ThePropertyName = TheNewPropertyValue
Command1.Caption = "Hello"
This code will do exactly the same as the code line above it.
Note that MyVar is without quotes, because it's variable.
Form1.Caption = "Hello"
Command1.Visible = False
Form1.Enabled = True
0 - Standard
1 - Graphical
Command1.Style = 1
Form1.WindowState = 2
MyButton.Picture = LoadPicture("d:\games\toto.ico")
Setting the BackColor Property
You can set the BackColor property at run-time in 2 ways.
Command1.BackColor = &H000000FF&
Figure 10
Figure 11
The color value will be displayed in the BackColor Property cell (Figure 13).
Figure 13
Command1.BackColor = &H000000FF&
Command1.BackColor = vbRed
And so on...
vbBlue = 5
Piano = "gggg"