Advertisement
Promo

Application development Toolkit

Working with events in C#

Harold Davis Builder.com

Published: 01 Oct 2002 15:21 BST

  • Email
  • Trackback
  • Clip Link
  • Print friendly
  • Post Comment

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.

You may not have a System.EventArgs to pass the event; for example, you might be invoking it outside an event. But that's not a problem, since you can create one and invoke an event method as follows:

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

Next

Previous

1 2


  • Email
  • Trackback
  • Clip Link
  • Print friendlyPrint with EPSON

Did you find this article useful?


Full Talkback thread

0 comments

Video icon

Video

Discussions

Jake Rayson Jake Rayson

Attack Site!

Wednesday 30 December 2009, 4:30 PM

3 comments
Tezzer Tezzer

So?

Wednesday 30 December 2009, 3:05 PM

2 comments
Tezzer Tezzer

So Much for Microsoft "Making Nice" wi...

Wednesday 30 December 2009, 3:01 PM

5 comments

Win a Creative Zen X-Fi2 player and accessories

Win a Creative Zen X-Fi2 player and accessories

What is ZDNet UK's usual tagline?

Competition closes - 14 Jan 2010


Skip Sub Navigation Links to CNET Brand Links

Help

Become part of the ZDNet community.

Newsletters