Share via


Visual Basic Concepts

Responding to Events with Add-Ins

As a Visual Basic programmer, you're accustomed to handling events such as a mouse move, a mouse click, the selection of an item in a ComboBox, and so on. In the Visual Basic version extensibility model, you are given an additional level of control — the ability to respond to events that occur in the IDE itself, such as when a user selects a project in the Project Explorer window or highlights code in a module. The Events object is your gateway to these events.

The Events object, which is referenced directly from the root VBE object, supplies methods that allow add-ins to connect to all events in the extensibility model. (Note that events can also occur in each object.)

The Events object contains the following objects:

  • CommandBarEvents

  • FileControlEvents

  • ReferencesEvents

  • SelectedVBControlsEvents

  • VBComponentsEvents

  • VBControlsEvents

  • VBProjectsEvents

Each of these objects allows you to respond to events that pertain to that object. For example, the CommandBarEvents object allows you to respond to events that occur to CommandBar objects, such as when a command bar is clicked. The SelectedVBControlsEvents objects allows you to respond to events that occur to any currently selected Visual Basic controls, and so on.

The WithEvents keyword exposes these events. For example, the VBComponentsEvents object has the ItemAdded event, which occurs when a component is added to a project, the ItemRemoved event, which occurs when a component is removed from a project, and so on.

Here's an example of how to declare such an object in a class module:

Private WithEvents evtVBProjects As VBProjectsEvents

In order to gain access to these events, however, the object must be referenced. Let's examine and reference one such object, the VBControlsEvents object, which is a property of the VBIDE.Events object. It has three events: ItemAdded, ItemRemoved, and ItemRenamed, and two parameters, VBProject, and VBForm.

In order to gain access to the ItemAdded, ItemRemoved, and ItemRenamed events, you must first reference the VBControlEvents object using the Set statement in the Declarations section of the main class module:

' Expose the Events.
Public WithEvents CtrlHandler As VBControlsEvents

' Gain access to the events by referencing the object.
Set Me.CtrlHandler = gVBInstance.Events.VBControlsEvents(Nothing, Nothing)

You now have programmatic access to the ItemAdded, ItemRemoved, and ItemRenamed events, and you now can build an add-in that can respond when a user adds, removes, or renames a control in a project. How it responds to these events is up to you and the code you place in the event procedures.

The arguments in the previous Set statement are set to Nothing. This means that we are not referring to a specific form in a project, but rather to any form in any project. You can consider the Events objects' arguments as a sort of filter which determines which events in a project, component, or control you're interested in monitoring. If you specify a particular project and form when setting an instance to a VBControlsEvents object, for example, control events are raised only for that particular form in that particular project. If you set both values to Nothing, however, all control events for all forms in all associated projects are raised. In other words, you are not filtering any of the control events.