Colors and Background Colors

Visual Basic has a Color class. The color class has many color objects that represent different colors, like Color.Green, Color.Yellow, etc.

All Controls have a BackColor property that can be used to set the background color for the control. The background color is the color on the surface of the control. The ForeColor of the control is the color that is used for text that appears on the control. These properties give the foreground and background color of a control.

Let us write a program that experiments with colors. The program will have a form with two buttons, one labelled Green and the second labelled Blue. Clicking a button will change the background color of the form to the corresponding color. You can see what the form will look like when the program starts to run in the screen shot to the above. You can also see the effect of pressing the two buttons in the screen shots that to the right and below.

The code is simple. Just set BackColor property of the form in the two methods of the form that have been designated as event-handlers for the buttons.

The code for the event handlers is shown below.

Public Class Form1
    Private Sub GreenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GreenButton.Click
        BackColor = Color.Green
    End Sub

    Private Sub BlueButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlueButton.Click
        BackColor = Color.Blue
    End Sub
End Class