Writing Code Behind a Control


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

Public Class Sheet1   Private Sub Button1_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles Button1.Click   End Sub   Private Sub Button1_SystemColorsChanged( _     ByVal sender As System.Object, ByVal e As System.EventArgs) _     Handles Button1.SystemColorsChanged   End Sub End Class 


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.




Visual Studio Tools for Office(c) Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
ISBN: 0321411757
EAN: 2147483647
Year: N/A
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net