An event is a class member that activates when triggered by a button click or similar. Events can fire off almost any response on any object. Event methods track input, such as button-clicks, and can report such action across methods.
In this part of our introductory series on C#, I'll cover the basic elements of working with events in the context of a Windows form and how to use Visual Studio to add procedures to handle a form event, also known as an event handler. I'll also examine the code behind an event method, so that you can add an event strictly in the code editor without using the visual development environment.
What is an event?An event is a placeholder for code that is executed when the event is triggered, or fired. Events are fired by a user action, program code, or by the system. The event method, or event procedure, formally consists of the procedure name followed by two arguments. The first argument, or parameter, is the object firing the event; the second argument is of the type System.EventArgs. In addition, you must use the += operator to hook up the event method as a delegate, which is a method that stands in for another method. You may be familiar with += in its role as the C# additive assignment operator. To make this more concrete, let's look at how a form click event for a form class named Form1 would be wired. (You'll find this code in the InitializeComponent method in the "hidden" region of Windows Form Designer generated code):
this.Click += new System.EventHandler(this.Form1_Click); The related framework for the event method is:
private void Form1_Click(object sender, System.EventArgs e) {
} It's easy to invoke one event from another event. For example, the following code displays a message box when the Enter event of button1 is fired: private void button1_Enter(object sender, System.EventArgs e) {
MessageBox.Show ("The Button's Enter event has been fired",
"C# for Newbies", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);}
To invoke the button1 Enter event from another event, add a call to the button1_Enter method in the other event. In a form's DoubleClick event, the code would be: private void Form1_DoubleClick(object sender, System.EventArgs e) {
button1_Enter (sender, e);
} Now when the form is double-clicked, the message box displayed by firing the button's Enter event will appear, as shown in Figure A.
| Figure A |
![]() |
| Events are handy for launching notifications such as this one. |
button1_Enter (this, g);







