BackColor, ForeColor Properties
See Also Example
Form, check box, combo box , command button (BackColor only), data control , directory list box , drive list box , file list box , frame , grid , label , list box , OLE control , option button , picture box , Printer object (ForeColor only), shape (BackColor only), text box .
BackColordetermines the background color of an object.
ForeColordetermines the foreground color used to display text and graphics in an object.
[form.][control.]BackColor[ = color ]
[form.][control.]ForeColor[ = color ]
Visual Basic uses the Microsoft Windows environment RGB scheme for colors. Each property has the following ranges of settings:
Range of settings Description
Normal RGB colors Colors specified by using the Color palette , or by using the RGB or QBColor functions in code.
System default colors Colors specified with system color constants from CONSTANT.TXT, a Visual Basic file that specifies system defaults. The Windows environment substitutes the user's choices as specified in the user's Control Panel settings.
For all forms and controls, the default settings at design time are:
BackColorset to the WINDOW_BACKGROUND system default color as specified in CONSTANT.TXT.
ForeColorset to the WINDOW_TEXT system color as specified in CONSTANT.TXT.
In labels and shapes, the BackColor property is ignored if the BackStyle property is 0 (Transparent).
If you set the BackColor property on forms or picture boxes, all graphics and print output, including the persistent bitmap Setting the ForeColor property does not affect graphics or print output already drawn. On all other controls, the screen color changes immediately., are erased.
The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF). The high byte of a number in this range equals 0; the lower three bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (&HFF). If the high byte is not 0, Visual Basic uses the system colors, as defined in the user's Control Panel and enumerated in CONSTANT.TXT.
To display text in the Windows environment, both the text and background colors must be solid. If the text or background colors you've selected are not displayed, one of the selected colors may be ditheredthat is, comprised of up to three different-colored pixels. If you choose a dithered color for either the text or background, the nearest solid color will be substituted.
Creating Custom Colors
Chapter 15, "Creating Graphics for Applications"
BackColor, ForeColor Properties Example
The example resets background colors randomly twice each second for a form and picture box. To try this example, paste the code into the Declarations section of a form that contains a picture box control and a timer control. Then press F5.
Sub Form_Load ()
Timer1.Interval = 500
Sub Timer1_Timer ()
BackColor = QBColor(Rnd * 15)
Picture1.BackColor = QBColor(Rnd * 15)