views:

40

answers:

3

Usually when I subscribe to an event, I use the Visual Studio builtin function to generate the method. So if I want to bind a clicked event to a button after I write += I click tab one time to generate the code after +=, and then tab again to create the empty method associated with this event.

So for a button clicked event, I will end up with something like this:

button.Clicked += new EventHandler(button_Clicked);

void button_Clicked(object sender, EventArgs e) {
  throw new NotImplementedException();
}

Since I prefer the shorter syntax for binding the eventhandler, I always go back to the autogenerated line, and change it to look like this:

button.Clicked += button_Clicked;

My question is simply. Are there any way to make VS automatically prefer this syntax over the default one, so I don't manually have to go and change this every time.

This applies both for VS2008 and VS2010

+1  A: 

As far as I know, no.

I'm sure I've read it somewhere authoritative, but I can't remember where right now

Benjol
+2  A: 

No, this is not under your control to modify.

It is easier for them to keep it in the old style so that it always works no matter which version of C# is being targeted. Otherwise they would have to make the generated code conditional on the C# version and I can imagine that is just more work than it is worth. Unfortunately this code generation is not extensible so you will need to modify your code manually yourself.

You could try third-party extras such as the ReSharper product to gain extra productivity as they implement many cool features by accessing the object modal and modifying it.

Phil Wright
I was afraid so, but it was worth a shot. Thanks for letting me know for sure.
Øyvind Bråthen
+1  A: 

This annoys me as well. However I use ReSharper which offers a few choices when creating event handlers such as creating a new method, adding a lambda or anonymous method, or using any existing methods that have the appropriate signature.

Also, R# will highlight any redundant code and let you remove it easily, either from a single site or from the entire project/solution.

Drew Noakes
ReSharper is maybe a solution here. Thanks.
Øyvind Bråthen