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?
views:
189answers:
4
+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
2009-10-30 14:52:40
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
2009-10-30 15:00:40
Well yes, following his usage of "VB" == "VB.Net in VS", I kinda assumed he would be using VS.
Robert Giesecke
2009-10-30 15:28:55
+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
2009-10-30 14:56:23
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
2009-10-30 15:44:24
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
2009-10-30 15:48:39
Nice answer, I'd forgotten you could remove the "new EventHandler" code when you use lambdas.
Matt Warren
2009-10-30 15:55:14
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
2009-10-30 15:55:37
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
2009-10-30 17:05:55
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
2009-10-30 17:09:27