BackColor, ForeColor Properties

See Also3H2I090              Example23TCZHM>Low

Apply To

Form14TJ2LN, check box9P3BU5, combo box1YZXFF6, command buttonXJSPC0 (BackColor only), data control2E1FEX3, directory list boxO9U5A0, drive list box5WJO0PW, file list box1M6S8UX, frame1KX6ZP8, grid2VGT0PT, label3MNIZ8D, list boxG11UCK, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, Printer objectCBQUDQ (ForeColor only), shape9JZFLA (BackColor only), text boxYPYZDG.


         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 palette11468R9, or by using the RGBLANRGB or QBColorNYS7SU 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 bitmap41CTZFK, are erased.  Setting the ForeColor property does not affect graphics or print output already drawn.  On all other controls, the screen color changes immediately.

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.

See Also


Creating Custom ColorsCJ5GBZ

FillColor PropertyL2TW7M

FillStyle Property2N3S7OE

Setting ColorsXBS80I


Programmer's Guide

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

End Sub


Sub Timer1_Timer ()

   BackColor = QBColor(Rnd * 15)

   Picture1.BackColor = QBColor(Rnd * 15)

End Sub