Writing code behind a control on a document or spreadsheet is not much different from writing code behind a normal Windows Forms control. You can double-click a control, and the designer will add a new event handler for the default event on the control in the partial class for the sheet or document you are working on. Event handlers can also be generated by using the Events view in the Properties window. In this view, you can double-click an event-handler cell to add a default named event handler for an event. Alternatively, you can enter the name of the event-handler function you want to use. Listing 14.2 shows the code generated when you drop a button on a spreadsheet and then double-click the event-handler cell for Click and SystemColorsChanged to generate default event handlers for these events. Listing 14.2. Default Event Connection and Handlers Generated by VSTO for a Button's Click and SystemColorsChanged Events
Not all the events on a Windows Forms control are raised in an Office document. The ResizeBegin and ResizeEnd events, for example, are common across all Windows Forms controls (these events are defined on the Control base class) but are never raised on controls on a document or worksheet because of the way that Windows Forms support in VSTO was designed. |