Braintique.com header
Left Navigation Bar

Understanding C# Events

And now, a message from our sponsors...No, seriously folks, let’s stop back for a second, and ask the question, “Just what is an event?”

Generally, an event is a placeholder for code that is executed when the event is triggered, also called firing an event. Events are fired by a user action, program code, or by the system.

From a more formal—and a C#—perspective, an event is an object member, specifically a method. (Note that the discussion in this article primarily focuses on Windows forms but equally applies to other objects such as Buttons and other controls.)

The event method, which is also called an event procedure, formally consists of the procedure name followed by two arguments. The first argument, or parameter, is the object firing the event, and the second an argument of the type System.EventArgs. In addition, the event method must also be hooked up as a delegate, or a method that stands in for another method, using the += operator. (You may also be familiar with += in its role as the C# additive assignment operator.)

To make this more concrete, here’s 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 would be:

private void Form1_Click(object sender,
System.EventArgs e) {

}

You should also know that it is 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, for example, a form’s DoubleClick event:

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 is displayed.

Enter Event

Note that it is not a problem if you don’t have a System.EventArgs to pass the event (you might be invoking it outside an event). You can create one and invoke an event method as follows:

System.EventArgs g = new System.EventArgs();
button1_Enter (this, g);

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write