Programming Against Events

VSTO lets you program against events of host items and host controls. Events are provided for the NamedRange, ListObject, XMLMappedRange, and Chart controls. Many of these events have been added by VSTO. For example, a native Range object does not have any events, but the VSTO NamedRange object exposes several. You no longer need to listen for an event on the worksheet and then map the event to a particular cell or range of cells on the worksheet; instead, VSTO does this mapping for you by reraising the event on the appropriate host control. VSTO also adds events to the Worksheet and Workbook host items.

In some cases, the name of the event has been changed if a method with the same name already exists. For example, the worksheet has an ActivateEvent event and a Deactivate event. The Activate event has been changed to ActivateEvent because there is an Activate method for a Worksheet object. The Deactivate event is not renamed DeactivateEvent, however, because there is no corresponding Deactivate method.

Because code is typically run in response to a user's action, it is important to be able to write code to respond to events of these host items and host controls. For example, if users right-click a cell, they see a custom context menu in which they can take further action. Or if a new row is added to a data-bound list object and an error occurs, users are told how to fix the error. In this section we look at the events for each of the host controls and host items available in an Excel project.

0 0

Post a comment