Welcome

You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

Creating Event Handlers in C# The Definitive Guide

Posted by Keith Elder | Posted in .Net, Visual Studio | Posted on 07-07-2008

Ok Chris Love, this one’s for you.  Here is how you create an event handler in C# using Visual Studio.

Here is a form I built in a matter of seconds.  I know, we’ll discuss my mad design skills later.

image

Let’s pretend I was in the code behind and wanted to create a Form Load event.  In the code behind within the constructor start typing until you find the event you want to create as shown here:

image

Once located press <Enter> to auto complete the name.  Here is the step that a lot of people miss.  Type the operator += and watch what happens.

image

Visual Studio will give you a notification to press TAB to insert the EventHandler.

Pressing TAB will then give you this:

image

It is at this point if you press TAB again, the callback method will be generated.  Thus when you do += press TAB TAB (2 tabs).

image

How many key strokes was that?  Let’s see.  I had to type in my form the characters:  this.Lo <enter> += TAB TAB.  Easy quick and simple.

Comments (17)

Eish guys I’m currently busy in C# 2008 Express edtiion did all well but events handlers cant be fired when I press the buttons to display something on the list box, even when I check boxes event handlers dont respond any who can help as to what cud be the problem, the computer or myb de software dat I’m using nor program I’m busy with, any code cud help alot….

“Mad design skills”… Love it!

This is good but not so fully to understand.

Dear Mr Elder,
3 months ago I have started my project. First , interfacing a PIC microcontroller to USB.
I have now succesful communication with PC.
I have no idea how to acomplish my event handler cause i am noobie in VCC++
Just if you give me some clues how to start.
I did try with timers but minimum time is 10 ms, so slow for PIC speed.
🙁
Thanks for your reply.
Regards
Carlos

You’ll need to look at creating / wiring up your own event handler and delegate. Should be plenty of samples on MSDN on how to do it.

Just a question ,
If I want to generate an external event handler ???
Example:
A byte from external USB device is true (or logic 1), for each (true), I want to increment a counter inside a labelbox. No click mouse , no keystroke , just de (true) data from USB must fire this event.
Any one knows the answer?
I really buy a SIX beer pack.
🙂

Ah the joys of event-handling. I stumbled into a “no-code” way of doing this I’ll share; you all owe me a beer.

There’s two simple ways of doing this.

1) click the object, then click the even icon (the lightning bolt icon). Locate the event on the list. Click the blank space to the right. Type in the name of a handler (you make it up). Hit return and the event-sender is automatically created in the hidden code and an event handler is automatically created in your code.

2. This works nice when you wish to tie multiple controls (like radio buttons) to a single event handler. Write the blank event handler with the correct arguments. Go back to the form, click the control/object to highlight it. Click the lightning bolt for events. Find the event in the list (i.e. CheckedChanged for a radio button). Click the pull-down and select the handler from the list. The event sender is automatically created.

Tah Dah! Like magic; event handlers made easy. You can buy me the beer next time we get together.

Paul (aka Paul1307 in some circles).

this website is so interesting that i have been learning so much informations and tutorial.
thanks for you……..

@Nate

I’m not really sure why you are going negative when this DOES work in Asp.Net.

Open the code behind file of a page and type this.Init you should see Init because that is a standard event on the Page.

Even on a control in design mode you can see what events it fires.

You *may* want to go back and take another look. If you still don’t see it, hit me on IM (see about page) and we’ll do a SharedView.

-Keith

Unfortunately, this method doesn’t work in ASP.NET. You can type “this.” all day long, and you won’t get the intellisense list to show you available events to handle. So one must wonder…

Why Microsoft, would you give us functionality in a particular language on the Windows development side but not on the Web development side when it is clearly available in BOTH sides of another language? This is just sloppy IDE design, IMO.

When I switched from VB I felt the same way Chris did, until the second day when I realized I could wire up events without my hands ever leaving the keyboard. I can’t imagine going back…

In web apps ASP.NET has gone a step further and ties up the event with the correspondent handling method for you automagically thanks to the page directive AutoEventWireup. All you need is a method with the proper signature and name and the rest of the work is done by the framework. Again one of the ASP.NET evil defaults, alongside Viewstate=true and sessionState mode InProc.

@Shawn

I use that sometimes but primarily for web services to handle events inline. For windows forms where there are A LOT of events I use the other method because I can put a region around all of them and segment them out in the code. Much cleaner that way to me.

I prefer to use Lambda’s to not pollute the method signature:

this.Load += (s,e) =>
{
// Do Something
};

@Chris
Are you putting a space after the += ? I don’t think the UI triggers until that is done. I know it works. If you want to do a SharedView go to my About page on the site and connect with me on MSN.

If you want to look at the events on an object go into the events in the properties window. They are all listed there. They aren’t listed in the code viewer though and I agree that is a handy feature but instead C# IDE puts all of the methods and properties you have declared.

Thnx Keith, but this is still error prone and not intuitive. What if I want to know the potential events? What if I do not have the method signature handy in my brain, I mean we have many events that pass custom EventArgs types. Plus I still cannot get the TAB TAB to produce anything for me. :<

Too bad the IDE doesn’t reduce it to:

Load += Form1_Load;

With the advent of C# 2.0 in 2005, the EventHandler delegate declaration is no longer needed. That would save a couple more keystrokes.

Write a comment