Mike
Mike

Reputation: 5509

Is it possible to automatically hook up events in C# using VS2008?

I recently switched over to C# from vb.NET and within visual studio found that hooking up events is extremely annoying. In VB I could select a control from a drop down on the top left and the event on the top right drop down and the method would automatically be created and attached to the control using "handles". I know that is not supported in C# but it seems I have to go through each control and add the events manually on the page and the codebehind. Is there an easier/faster way to do this like in VB or is it just how it is? Thanks!

Upvotes: 1

Views: 355

Answers (2)

Donut
Donut

Reputation: 112825

Yes, there is! Click a control in Design view, then click on the "Events" button in the Properties window (see 1 in hand-annotated diagram below).

From here you can see a list of all the events available to that control. By typing a method name (see 2) and pressing enter, Visual Studio will create a method (if it doesn't already exist) and hook it up properly.

Alternatively, double-clicking in the field where you would type in the handler name causes Visual Studio to assign a default value.

alt text

Upvotes: 11

Kasper Holdum
Kasper Holdum

Reputation: 13363

You can setup events extremely fast in C# compared to VB. In the code window type the name of the instance and then event name then write += and press tab twice. That will hook up the event and create a method name accordingly that will handle the event.

For example write:

panel1.MouseClick +=

and then press tab once to insert the eventhandler and twice to both insert eventhandler and create the method for it.

alt text http://img136.imageshack.us/img136/7514/eventhandlercsharp.png

Upvotes: 3

Related Questions