Popular Posts

Showing posts with label Visual Basic lesson3. Show all posts
Showing posts with label Visual Basic lesson3. Show all posts

Thursday, March 5, 2009

Setting the BackColor Property (Continue)

Setting the BackColor Property (Continue)

Way 2: Using the Colors Constants
First of all, what is Constant?
Constant is a variable that its value can not be changed.
Constant holds a common used value.
For example, the Constant vbRed holds the value
of the red color - &H000000FF&

Instead of writing in your code:

Command1.BackColor = &H000000FF&

You can write:

Command1.BackColor = vbRed

The 2 statements above are identical, because
vbRed = &H000000FF&


Where are these constants came from?
Visual Basic automatic declare them
when the program starts.

Imagine it like as the following code is
automatic being entered to your program:

Dim vbRed As Long
vbRed = &H000000FF&

Dim vbBlue As Long
vbBlue = &H00FF0000&

And so on...

But with one exception: You can't
change the constants value.

For example, the following code is NOT allowed:

vbBlue = 5


There are more Color constants, a partial list:

vbRed, vbBlue, vbBlack, vbGreen, vbWhite, vbYellow


You can declare your own constants.
The constant declaration syntax:

Const MyVariableName = MyVariableValue


For example, the following code will declare a const
variable with the name Piano and the (const) value "abcdef"


Const Piano = "abcdef"


After this declaration, the Print Piano
code line will print abcde on the form.

In addition, the following code line will not be allowed:

Piano = "gggg"

Because it's been declared as a Const,
and Const value can not be changed.


To learn more advanced programming techniques like
conditional statements, go to the Conditional Statements Tutorial.

Setting the BackColor Property

Setting the BackColor Property
You can set the BackColor property at run-time in 2 ways.

Way 1: Using the Color's numeric value
Every color has numeric value.
You can simply assign this value to the BackColor property.

For example, The numeric value of the red color is &H000000FF&
If you want to set the Form BackColor property to red,
use the following code:

Command1.BackColor = &H000000FF&

You can find what is the numeric value of every
color by making the following simple steps:

Click on the BackColor property arrow button
in the properties window (Figure 10).

Figure 10



Then click on the Palette Tab (Figure 11).

Figure 11



Select your desirable color (Figure 12).

Figure 12


The color value will be displayed in the BackColor Property cell (Figure 13).

Figure 13




And once again, if you setting a Command Button's color,
don't forget to set its Style property to 1 - Graphical.

To learn the second way, you'll have to learn
a little bit about Constants.
All of this in the next page...

Setting Properties At Run-Time (Continue)

Setting Properties At Run-Time (Continue)
Not all the properties get text values.
For example, Visible property can be "True" or "False"

False and True are not text strings, but
Visual Basic commonly used values that called "Boolean"

Therefore, when you assign these values to property,
You don't have to use the quotes, For example:

Command1.Visible = False
Form1.Enabled = True


Some of the properties values has the following syntax:
Number - String

For example, the Command Button's Style property can
get 2 values:

0 - Standard
1 - Graphical


To set these properties value, omit the string.
For example:

Command1.Style = 1
Form1.WindowState = 2


Some properties represent graphics, for example
The Picture property.

To set these properties at run-time, use
the LoadPicture Command.
The example below will load the ICO file "d:\games\toto.ico"
to the Picture property of Command Button with the name "MyButton"

MyButton.Picture = LoadPicture("d:\games\toto.ico")

Setting Properties At Run-Time

Setting Properties At Run-Time
By now you know how to set a component
property before running your program (=at "Design Time"),
Now we will learn how to do that during program's Run-Time.

To change a property, use the following syntax:

TheControlName.ThePropertyName = TheNewPropertyValue

For example, suppose we have Command Button
With the name "Command1", and we want to
set its Caption property to be "Hello".
To do that, we will use the following code:

Command1.Caption = "Hello"

To test it, simply copy the line above to your
Command1 Click event, run the program
and click the button at run-time.

Note that the Hello is inside quotes because
it's a String.
You can assign a variable value to a property:

Dim MyVar As String
MyVar = "Hello"
Command1.Caption = MyVar

This code will do exactly the same as the code line above it.
Note that MyVar is without quotes, because it's variable.

And how do you set a Form caption property at run-time?
exactly the same:

Form1.Caption = "Hello"

The Form's KeyPreview Property

The Form's KeyPreview Property
To understand this property,
lets look on the following example:

Start new project, and add 1 Command Button
(named Command1) to your form.
verify that the Form's KeyPreview property is set to "False".
Add the following code to your program:


Private Sub Command1_KeyPress(KeyAscii As Integer)
Print "Button Pressed"
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
Print "Form Pressed"
End Sub

The code above will print "Button Pressed" on
the form when the Command Button's KeyPress event
will be executed, and print "Form Pressed" when the
Form's KeyPress event will be executed.


Run the program and press any key on the keyboard.
"Button Pressed" is appearing on the form, but
"Form Pressed" isn't appearing.

The Form's KeyPress event hasn't been executed.

When the KeyPreview property is "False",
if any control is found on the form (the command button in this case),
It will get all the Key events (KeyPress, KeyDown and KeyUp)
instead of the form.

To allow the Form's Key events be executed,
set the KeyPreview property to "True".

Lets try it. Set the KeyPreview
property to "True", run the program again
and press any key.
The Form's KeyPress event has been executed,
in addition to the Button's KeyPress event that
been executed too.

Notice That the form's KeyPress event executed
before the Button's event.

Additional Form's Properties

Additional Form's Properties
The Form has many of the Command Button's properties
(MousePointer, BackColor, Visible and more).
He also has additional properties:

Caption - The text that appear in the Form's title bar,
and in the taskbar.

Icon - The Icon that appear in the form's title bar (Figure 7)
and in the Task bar (Figure 8).






ControlBox - Set this property to "False" to remove
the title bar's Close, Minimize and Maximize buttons (Figure 9)



MaxButton - Set this property to "False" to disable
The title bar Maximize Button.

MinButton - Set this property to "False" to disable
The title bar Minimize Button.

ShowInTaskbar - Setting this property to "False" will
cause the form not showing in the task bar.

WindowState - The initial appearance style of the form:
Minimized, Maximized, or normal.

Additional Command Button's Properties

Additional Command Button's Properties
Add 1 Command Button to your form,
and change the following properties to
see what they do.

Caption - the text that appear on the button

Font - The Caption's font (Figure 2).



ToolTipText - Insert into this property the text
that will appear when the mouse is stand still
on the button (Figure 3).



Enabled - Setting this property to "False" will make
the button be gray, and the user will no be able to press on it (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



Picture - The Picture that appear on the button (Figure 6).
This property and all the following Picture related properties will
take affect only after you will set the
Button's Style property to 1 - Graphical



DisabledPicture - The Picture that will appear on the button when
it disabled (When the Enabled property is "False", like in Figure 4).

DownPicture - The Picture that will appear on the button when
the button is being pressed.

Visible - If you will set this property to "False", the Button
will not appear on your form at run-time.

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.

Left - The Button X coordinate, relative to the form left border.

Top - The Button Y coordinate, relative to the form Top border.

Width - The Button width.

Height - The Button height.

MouseMove, MouseDown and MouseUp Events (Continue)

MouseMove, MouseDown and MouseUp Events (Continue)
Lets check out these events parameters.

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Each one of these events gets the same parameters:
Button, Shift, X and Y.


The Shift parameter is the same as the KeyDown
event's Shift paramater.

For example, if you'll press the Shift button while clicking
the mouse, the Shift value will be 1.


The Button parameter value is 1 if you've clicked
the left mouse button, and 2 if you've clicked the
right one.


The X and Y parameters are the X and Y coordinates of
the mouse cursor, relative to the upper left button corner.
The coordinates of the upper left button corner are (0, 0)
The coordinates of the Bottom Right button corner are (Button Width, Button Height)

You can try a little example.
Put the following line in the Command Button's MouseMove event:

Print X, Y

This line will print the X coordinate, and next to it
the Y coordinates (For example, the line:
Print "Hello", "World"
will print: Hello World).

MouseMove, MouseDown and MouseUp Events

MouseMove, MouseDown and MouseUp Events
Lets check out the Command Button's MouseMove,
MouseDown and MouseUp events.

We will do that like we've done before,
by inserting different Print commands to every event.

Copy the following code to your program:


Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Print "MouseDown"
End Sub

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Print "MouseMove"
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Print "MouseUp"
End Sub


Run the program.
Move the mouse over the form - nothing is happening.
Move the mouse over the Button - The Button's MouseMove event
is being executed every time you move the mouse over it, therefore
you see that "MouseMove" is being printed on the form
every time you move your mouse over the button.

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.

Learning about Parameters (Continue)

Learning about Parameters (Continue)
Look at the first line of the Command Button's KeyDown Event:

Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)

As you can see, the KeyDown event gets two parameters:
The KeyCode parameter, and the Shift parameter, both are
Integer type.

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.

If a sub is getting more than one parameter, the parameters
have to be separated with commas.
For example:

Private Sub MySubName (Parameter1 As String, Parameter2 As Integer, Parameter3 As String)

Lets check out the KeyDown event's parameters.

The first is KeyCode, and it holds the KeyCode value
of the pressed key.
The KeyCode value is usually different from the Ascii Value.

The different between KeyCode and Ascii, is that
every character has Ascii value (for example G, @, |, =, and more)
but there are some keys that don't represent any character, for example:
Alt, F4, Ctrl, The left arrow key.

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


The second parameter is Shift, and its value helps you
to determine if the user has pressed the Shift, Ctrl or Alt keys.
The Shift holds the value 1 if the user has pressed the Shift key,
the value 2 if the user has pressed the Ctrl Key,
and the value 4 if the user has pressed the Alt Key.

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:

If the user has pressed the "A" key,
The KeyCode parameter will hold the number 65, and
the Shift parameter will hold the value 0.

If the user has pressed Shift and "A" together,
The KeyCode parameter will hold the number 65, and
the Shift parameter will hold the value 1.


The KeyUp event's parameters are the same as the KeyDown event's parameters.

More About Ascii

More About Ascii
How can I know what is the Ascii value of a specific character?
Use the Asc command.
For example, the following line:

Print Asc("b")

Will print on the form the Ascii value of the character "b".

How can I know which character's Ascii value is 98?
Use the Chr command.
For example, the following line:

Print Chr(98)

Will print on the form the character that its Ascii value is 98.

Learning about Parameters

Learning about Parameters
Parameters are variables that being passed to a Sub.

Look at the first line of the Command Button's Click event:

Private Sub Command1_Click()

And at the first line of the Command Button's KeyPress event:

Private Sub Command1_KeyPress(KeyAscii As Integer)


The Click event's first line is ended with
empty parentheses () and the KeyPress event's first
line is ended with (KeyAscii As Integer)


What is the (KeyAscii As Integer) ?
It's a parameter that been passed to the KeyPress event.

This parameter is an Integer variable with the name KeyAscii.
Like if you've declared Dim KeyAscii As Integer

Why do we need this variable?
Because its value is very useful.

The KeyPress event is being executed when the user
press a key, and This variable holds the Ascii value of the
key that been pressed.
With This Ascii value you can know on which key
the user has pressed.

For example, the Ascii value of the "A" character is 65.
If the user has pressed the key "A" on the keyboard,
The KeyAscii parameter value will be 65.

Lets see an example.
Insert the following line to the Command1 KeyPress event:

Private Sub Command1_KeyPress(KeyAscii As Integer)
Print KeyAscii
End Sub

Run the program and press several keys.
You will see the Ascii value of every key you're pressing.

Notice that the KeyAscii values of "A" and "a" are differents.
Every characters has its own KeyAscii value,
and 2 characters that are the same letter, but have different case,
have different KeyAscii value.

The Command Button's KeyPress, KeyDown and KeyUp Events

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.

To try these examples, start a new project (as being
taught in Lesson 1).

Add 1 Command Button to your form. The Command
Button is called by default Command1.

Copy the following code to the code window (you
can copy and paste it using Ctrl + C for copying
and Ctrl + V for pasting):


Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
Print "KeyDown"
End Sub

Private Sub Command1_KeyPress(KeyAscii As Integer)
Print "KeyPress"
End Sub

Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
Print "KeyUp"
End Sub


When the Command Button's KeyDown event will be executed,
"KeyDown" will be printed on the form,
When the Command Button's KeyPress event will be executed,
"KeyPress" will be printed on the form, and when
the Command Button's KeyUp event will be executed,
"KeyUp" will be printed on the form.


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:



Lets see:
The first event that been executed is the KeyDown event,
because "KeyDown" was the first text that been printed on the form.

The second event was KeyPress, and then again KeyDown.
After every KeyDown event that been executed, a KeyPress
event had been executed.

We learnt that when a key is being holded down, the
KeyDown and the KeyPress events are being executed in
this order over and over again, until the key is up again.

When you release the key, the KeyUp event is being executed once