Brett Ryan
Brett Ryan

Reputation: 28255

Is it possible to alter the auto generated event handler code within visual studio?

I would like to be able to control the default code that's generated for event's when I use one of Visual Studio's automatically generated blocks. The current template is as follows:

void HandlerName(object sender, HandlerEventArgs e) {
    throw new NotImplementedException();
}

I would like to change this to the following:

private void HandlerName(object sender, HandlerEventArgs args) {
    throw new NotImplementedException();
}

Namely it's the args argument that I always change.

UPDATE: Further to this it is policy that we also include comments for private members here, thus another use-case for my requirement is to also generate the default comment.

UPDATE 2: I now retract the reasoning for wanting to rename e to args due to evidence of a non-standard naming convention, however I still would like to override the template if possible for explicit access modifier and default comments.

Upvotes: 9

Views: 1019

Answers (3)

Samuel
Samuel

Reputation: 12341

In the case you still want to change something to the auto-generated code for event handler, I use this extension for Visual Studio since a couple of years.

It's very helpful to generate more clean event handler name. As you will see, instead of generating something like txtName_LostFocus, the extension will suggest to you a method with name like this: OnNameLostFocus.

This help getting rid of underscore and prefixes that we usually add to controls. The extension have a configuration screen to add all the prefixes that you want.

But everything I say is not helpful for answering your question. Here is where it help -> In the code that you can download, you can see how the engine works to replace the name of the event handler method and I'm totally sure that you can found a solution to rename any argument you want (if you still want) and also, add an explicit modifier.

I will try to find the solution and let you know of my progress.

UPDATE: You can also use Resharper who will automatically if you want add explicit modifier for you using the clean up engine.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292465

I think the e comes from the delegate signature (delegate void HandlerEventHandler(object sender, HandlerEventArgs e)), so you can't change it without also changing the delegate signature...

Upvotes: 1

Kevin Wang
Kevin Wang

Reputation: 3330

Are you saying you want to change the auto-generated code when you create an event handler?

My answer is no, but I am not really sure why you would need to.

Upvotes: 0

Related Questions