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. The only difference is where the event handler is hooked up in the code generated by the designer. In a standard Windows Forms form, the event handler is hooked up in the hidden generated code (form1.designer.cs). In a VSTO code item, the event hook up is generated into the visible user partial class (sheet1.cs rather than sheet1.designer.cs) in the InternalStartup method.

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. The event handler hookup code is generated in the same place (InternalStartup) as if you double-clicked on the control. 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 Hookup and Handlers Generated by VSTO for a Button's Click and SystemColorsChanged Events

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.OfficeTools.Interop.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ExcelWorkbook1
 public partial class Sheet1
 private void Sheet1_Startup(object sender, EventArgs e)


 private void Sheet1_Shutdown(object sender, EventArgs e)

 #region VSTO Designer generated code
 private void InternalStartup()
 this.button1.Click += 
 new System.EventHandler(this.button1_Click);
 this.button1.SystemColorsChanged += 
 new System.EventHandler(this.button1_SystemColorsChanged);
 this.Shutdown += new System.EventHandler(this.Sheet1_Shutdown);
 this.Startup += new System.EventHandler(this.Sheet1_Startup);

 private void button1_Click(object sender, EventArgs e)


 private void button1_SystemColorsChanged(object sender, EventArgs e)



Events That Are Never Raised for a Control in an Office Document

Not all the events on a Windows Forms control are raised in an Office document. For example, the ResizeBegin and ResizeEnd events 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 the Windows Forms support in VSTO was designed.

Part One. An Introduction to VSTO

An Introduction to Office Programming

Introduction to Office Solutions

Part Two. Office Programming in .NET

Programming Excel

Working with Excel Events

Working with Excel Objects

Programming Word

Working with Word Events

Working with Word Objects

Programming Outlook

Working with Outlook Events

Working with Outlook Objects

Introduction to InfoPath

Part Three. Office Programming in VSTO

The VSTO Programming Model

Using Windows Forms in VSTO

Working with Actions Pane

Working with Smart Tags in VSTO

VSTO Data Programming

Server Data Scenarios

.NET Code Security


Part Four. Advanced Office Programming

Working with XML in Excel

Working with XML in Word

Developing COM Add-Ins for Word and Excel

Creating Outlook Add-Ins with VSTO

show all menu

Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
ISBN: 321334884
Year: N/A
Pages: 214
Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net