Wednesday, December 12, 2012

How many ways are there to specify colors in VB?


There are 4 ways to specify colors in VB:
1.  Hexadecimal numbers: It represents a Hexadecimal value that corresponds directly to one of the 16 million possible colors that can be displayed on a color monitor. If the number starts with &H00, then the remaining 6 Hexadecimal characters represent one of 16 million colors. These colors can be displayed by varying the intensities of Red, Green, or Blue. 2 Hex characters represent the intensities of Red, Green and Blue, and their values range from 0 to 255 Decimal, or 0 to FF in Hex.
An example will make this easier to understand.For instance, the hex value &H000000FF&  represents the color Red.
2.  Color Constants: Color Constants are a subset of the much larger universe of Visual Basic Intrinsic Constants. The idea behind the Visual Basic Intrinsic Constants is to give you a name that you can remember in place of a hard to remember value.
Constant         Value              Description
vbBlack           &H0                Black.
vbRed             &HFF              Red
vbGreen          &HFF00          Green
vbYellow        &HFFFF         Yellow
vbBlue             &HFF0000      Blue
vbMagenta      &HFF00FF    Magenta
vbCyan            &HFFFF00    Cyan
vbWhite          &HFFFFFF    White
3.   The QBColor Function: The QBColor function is a “holdover” from the original version of
Basic. The QBColor expands on the eight colors provided by the Color constants and gives you sixteen. The QBColor function req uires a single argument, which is a number ranging from 0 to 15. The QBColor function returns a value that can then be assigned to any of the color properties. For instance, this code returns a value which when assigned to the BackColor property of the form changes it to Green.
Form1.BackColor = QBColor(2)
4.  The RGB Function: The RGB function , while not extremely user friendly, gives you the ability to generate any valid color property value.
Here is the syntax for this function:               RGB(Red,Green,Blue)
As you can see, the RGB function requires three arguments, with valid values for each being 0 to 255.
Form1.Backcolor = RGB(255,0,0)
Form1.BackColor = RGB(Red:=0, Green:=255, Blue:=0)