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

Your comment:






 
 
 
Please add 6 and 7 and type the answer here:
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456