Excelnoobster
Excelnoobster

Reputation: 129

Visual studio 2015 c# - how can I autogenerate stub for a new event handler?

I'm creating a background worker in code, and want to subscribe to the DoWork event.

In visual studio designer, I can plonk a background worker on the form, then, to subscribe to the DoWork event I can double click the DoWork Event in the properties : Visual Studio will autogenerate an event handler stub with correct signature.

There is no corresponding means of achieving this in code.

If I add the background worker and DoWork Event in code instead of using the designer, I do this ... I create a new background worker. I added a stub routine called BGW_DoWork, void, no parameters. To find out the signature, I type BGW.DoWork += new (followed by space). Visual Studio Autocomplete then lists some suggestions, with the DoWorkEventHandler automatically selected, to which there is an information box to the right describing the signature of the delegate ( i.e. Delegate Void ; Object sender, System.ComponentModel.DoWorkEventArgs e ). After making make a mental note of the parameters, I manually type "object sender, System.ComponentModel.DoWorkEventArgs e " in the parameter list of the BGW_DoWork routine ( the param names could be anything of course, but I'm guessing this is th ususal convention). I change the "+= new" to "+= BGW_DoWork". et voila.

I find this a laboured way of adding the handler.

Are there other means of achieving this auto-generation? Or is there a way of copying the signature to clipboard, or maybe something else?

Upvotes: 0

Views: 172

Answers (1)

Tsahi Asher
Tsahi Asher

Reputation: 1810

You should just let Visual Studio generate the method stub for you. When you type "+=", press Tab twice, and VS will add the method to your code, where you can add your content to it. You don't have to write the stub yourself.

Upvotes: 2

Related Questions