«^»
11.3. Common uses of events

One common use of events is for registering methods to be executed when an event such as a click of a button in a GUI occurs.

Suppose we have:

using Button = System.Windows.Forms.Button;

A class can then declare a button using:

private Button iAddButton;

The constructor for the class can execute statements like:

iAddButton.BackColor = Color.Wheat;
iAddButton.Font      = new Font("Times New Roman", 8);
iAddButton.Text      = "Click Here";

where Color and Font are classes from the System.Drawing namespace.

The Button class also has an event property called Click. So if we want a method called iAddClick to be executed whenever there is a click on this button, we should also get the constructor to do:

iAddButton.Click += new EventHandler(iAddClick);

where EventHandler is a class of the System namespace.