Monday, 8 August 2011

Setting the BackColor Property

0 comments
 

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



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


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

Way 2: Using the Colors ConstantsFirst 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.

Leave a Reply