Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 2.61 KB

how-to-create-event-handlers-in-office-projects.md

File metadata and controls

51 lines (37 loc) · 2.61 KB
title description ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Create event handlers in Office projects
Learn about the several ways that you can create default event handlers for controls in Visual Basic and C#.
02/02/2017
how-to
VB
CSharp
Visual Basic [Office development in Visual Studio], event handlers
event handlers [Office development in Visual Studio]
Visual C# [Office development in Visual Studio], event handlers
events [Office development in Visual Studio]
John-Hart
johnhart
mijacobs
office-development

Create event handlers in Office projects

There are several ways to create event handlers in Visual Basic and C#. In design view, you can create the default event handlers for controls by double-clicking the control, or 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.

[!INCLUDEappliesto_all]

[!INCLUDEnote_settings_general]

To create an event handler in Visual Basic

  1. 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.

    [!NOTE] If you want to create event handlers for ThisDocument or ThisWorkbook, you must select (ThisDocument Events) or (ThisWorkbook Events) in the Class Name drop-down list

  2. 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 C#

  1. 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 afterwards. For example:

    this.<object name>.<event name> +=

  2. 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.

Related content