You have learned how to change properties at design time. In this lesson you will learn to change properties at run time. The basic idea is that when an event occurs (such as clicking or double clicking), some property (such as BackColor or Text) will be changed. We will write code (instructions) to be executed when an event occurs.
We are going to write a program that has two buttons. When you click the "Red" button, the form turns red. When you click the "Blue" button, the form turns blue.
Start a new project and build the form as shown in the illustration. The label
is named LblMessage, the buttons
are named BtnRed and BtnBlue. The text for BtnRed is &Red. The text for BtnBlue is &Blue. Preceding a letter with an ampersand makes that letter a "hot key":. typing R (or r) on the keyboard while the program is running is the same as clicking BtnRed.
After building the form as shown, double click on BtnRed. This will open the code view window. By default, the click event will be created, but with no code:
The first line says that this is the code for Form1. The cursor is ready between two lines of code. These two lines are the first and last lines of a procedure (Subroutine) named BtnRed_Click. The first line of the procedure has two arguments inside parenthesis: sender has information about what was clicked (BtnRed); e has information about the click (click or double click, if the shift key was down, etc.) After the parenthesis, there is a clause telling what event this procedure handles, or responds to (BtnRed).