Creating Event Handlers in C# The Definitive Guide


kick it on DotNetKicks.com

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.

posted @ Monday, July 07, 2008 6:08 PM

Print

Comments on this entry:

# re: Creating Event Handlers in C# The Definitive Guide

Left by Kevin Hazzard, MVP at 7/7/2008 6:13 PM
Gravatar

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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Chris Love at 7/7/2008 6:23 PM
Gravatar

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. :<

# re: Creating Event Handlers in C# The Definitive Guide

Left by Keith Elder at 7/7/2008 6:32 PM
Gravatar

@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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Shawn Wildermuth at 7/7/2008 7:21 PM
Gravatar

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

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

# re: Creating Event Handlers in C# The Definitive Guide

Left by Keith Elder at 7/7/2008 9:10 PM
Gravatar

@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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Val Minaev at 7/7/2008 10:25 PM
Gravatar

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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Mike Lasseter at 7/14/2008 8:17 PM
Gravatar

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

# re: Creating Event Handlers in C# The Definitive Guide

Left by Nate Southerland, MCSD .NET at 12/11/2008 5:35 PM
Gravatar

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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Keith Elder at 12/18/2008 8:16 AM
Gravatar

@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

# re: Creating Event Handlers in C# The Definitive Guide

Left by gerziher at 1/25/2009 6:30 PM
Gravatar

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

# re: Creating Event Handlers in C# The Definitive Guide

Left by Paul at 3/26/2009 2:08 PM
Gravatar

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).

# re: Creating Event Handlers in C# The Definitive Guide

Left by Carlos at 4/29/2009 9:00 PM
Gravatar

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.
:)

# re: Creating Event Handlers in C# The Definitive Guide

Left by TheElder at 4/30/2009 9:58 AM
Gravatar

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.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Carlos at 5/2/2009 12:53 AM
Gravatar

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

# re: Creating Event Handlers in C# The Definitive Guide

Left by Sagar Rawat at 5/19/2009 11:46 PM
Gravatar

This is good but not so fully to understand.

# re: Creating Event Handlers in C# The Definitive Guide

Left by Jordan Cobb at 10/14/2009 7:55 AM
Gravatar

"Mad design skills"... Love it!

Comments have been closed on this topic.
«February»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213