views:

189

answers:

4

I just recently started working with c# and was wondering if there is a simpler way to create event handlers for controls. For example if I have a button on a webform that i want a click handler for, I simply open the designer and double click it and it gets created and wired up for me. If I did not have have the option of using the designer, what other way would there be to create it other than by hand? For instance, in VB all the controls show up in the code window dropdown so you can select them, choose an event and it is stubbed out for you. Is there something similar in c# or am I stuck doing it the hard way?

+4  A: 

What is so hard about it? Just write

yourComponent.YourEvent += >Cursor is here<

And now you should see a hint that by hitting [tab], you'll get an implementation of a method for this particular event.

Nice and easy. No need at all to pollute the navigational dropdowns with stuff that isn't there. ;-)

Robert Giesecke
This approach assumes you're working in Visual Studio. I hope you're not in another editor, pressing tab with a look of puzzlement.
Programming Hero
Well yes, following his usage of "VB" == "VB.Net in VS", I kinda assumed he would be using VS.
Robert Giesecke
+2  A: 

You can do the work the designer does for you by yourself of course. Simply add:

button1.Click += new System.EventHandler(button1_Click);

and create a suited method to be called:

private void button1_Click(object sender, EventArgs e) { // button1 was clicked }
wroks
The only problem with this approach is you have to already know the signature of the event.
Jason
never mind.. i see that tabbing stubs it out for you so you dont have to know. that was my whole problem to begin with
Jason
A: 

You can use lambdas if you don't even want to create a function, if for instance the event handler is really simple.

You can do the following:

radioButtonLeft.Click += new EventHandler((sender, e) => CallSomeMethod(1));

That the easiest way I've found.

Matt Warren
A: 

Why is everyone suggesting a new EventHandler object??? Redundant

button1.Click += MyHandler;

private void MyHandler(object sender, EventArgs args)
{
}

or with C# 3.0

button.Click += (sender, args) => Handler();
Brian Genisio
Nice answer, I'd forgotten you could remove the "new EventHandler" code when you use lambdas.
Matt Warren
You do realize, that your code is nothing but syntactic sugar over new EventHandler(...)?And further, if you do not need the parameters, you could do this:someComponent.SomeEvent += delegate{ YourCall(); };
Robert Giesecke
Robert: Of course I realize this. My statement was that it is redundant. I am bringing it up for readability. Creating new EventHandler() is terribly ugly and redundant in our language. Just like GenericMethod<int>(5) is redundant and should just be GenericMethod(5) to increase readability. Lambda notation is also more readable, IMO, because it is immediately recognized by people familiar with other languages that include lambdas.
Brian Genisio
Yepp, that is true. I really don't like that VS is writing it that way when using my approach, btw. I thought you meant that there'd be some sort of runtime gain by not writing it the verbose way.
Robert Giesecke