How to: Create Event Handlers in Visual Studio Tools for Office
There are several ways to create event handlers in Visual Basic and Visual C#. In design view, you can create the default event handler for a control by double-clicking the control, or you can use the events pane of the Properties window to create handlers for any event on the control. However, if you are in code view, you may not want to switch to design view to create an event handler.
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see.
To create an event handler in Visual Basic
From the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for.
If you want to create event handlers for
ThisWorkbookin document-level projects, you must select (ThisDocument Events) or (ThisWorkbook Events) in the Class Name drop-down list. If you want to create event handlers for
ThisApplicationin application-level projects, you must select (ThisAddIn Events) or (ThisApplication Events) in the Class Name drop-down list.
From the Method Name drop-down list at the top of the Code Editor, select the event.
Visual Studio creates the event handler and moves the insertion point to the newly created event handler. If the event handler already exists, the insertion point moves to the existing event handler.
To create an event handler in Visual C#
Create the event delegate in the Startup event of the class by typing the qualified event name followed by a space, and then typing += with no space afterward. For example:
this.<object name>.<event name> +=
At the end of the line of code, press the TAB key twice.
Visual Studio automatically completes the line of code, creates the event handler, and moves the insertion point to the newly created event handler.