Writing Code in Visual Basic

The code that you will create will look like this when you are finished:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Public Class Form1
    'Programmer: Janet Joy
    'Change color of form with buttons
    Private Sub BtnRed_Click(sender As Object, e As EventArgs) Handles BtnRed.Click
        'change color to red
        Me.BackColor = Color.Red
        Me.Text = "You clicked red"
    End Sub

    Private Sub BtnBlue_Click(sender As Object, e As EventArgs) Handles BtnBlue.Click
        'change color to blue
        Me.BackColor = Color.Blue
        Me.Text = "You clicked blue"
    End Sub
End Class
CODE

You will actually type very little. All of the code except for the comments and lines 6, 7, 12 and 13 where provided for you.

In line 6 you start by typing me. (dot). As soon as you type the dot a list pops up. You can scroll down to backcolor and click it. Next you type = and another list pops up where you can select Color.Red. Backcolor can only be assigned a color, so as soon as we type the equal sign the pop-up box contains the values that can be assigned to a color property.

What the code means: The word me refers to the form. The dot separates an object from a child or a property. On line 6, we change the backColor property of the form to Red. The two statements that we have added are called assignment statement: they are used to "assign" a new value. In line 7 we have assigned a new value to the text property of the form. The text is a string and the value assigned to it is enclosed in double quotes..

Experiment: Run the program and click BtnRed. Add the code for BtnBlue.

Add more buttons to select other colors.

NEXT: Arguments