Introduction
Office has a user interface that has been designed to make it as easy as possible for an end user to access the functionality provided by each Office application. But the application that you are writing that is integrated with Office will have its own very specific user-interface requirements. The application you write will have user-interface needs that are not met by the default Office user interface.In previous versions of Office, Visual Basic for Applications (VBA) provided the ability to show User Forms to meet your application user-interface requirements. You could also use custom ActiveX controls on the document surface. Visual Studio Tools for Office (VSTO) adds Windows Forms control support to Office to meet your user-interface needs.
Moving from ActiveX to Windows Forms
When we started designing VSTO, being able to build applications that extended the default Office user interface was one of our primary goals. We also wanted to ensure that developers writing managed code would not have to rely on ActiveX controls to do so.NET developers want to use Windows Forms controls. To address these requirements, the team came up with a design to integrate Windows Forms deeply into Office. The vision was to allow you to use Windows Forms controls and forms in all the places you could use ActiveX controls and User Forms in previous versions of Office. We also wanted to make the design and coding experience similar to that of a traditional Windows Forms application.Chapter 15, "Working with Actions Pane."
When to Use Windows Forms Controls on the Document Surface
VSTO enables developers to put Windows Forms controls on the document surface. Just because you can put a control onto the document surface does not necessarily mean it is a good idea for your particular application. When should you use a control on a document as opposed to using a form, an intrinsic Office user-interface element such as a cell or a hyperlink, a custom menu command or toolbar button, a Smart Tag, or the actions pane?Think about how you expect the document or spreadsheet to be used and how you want to extend the interface. Maybe you are going to use an Excel spreadsheet as a front end to corporate data. For example, many stockbrokers use Excel as their primary input and display mechanism when trading. In this scenario, the spreadsheet is very rarely e-mailed or printed, so changing the spreadsheet interface to meet the application requirements makes a lot of sense. Putting a Windows Forms button control on the surface of the document meets the requirement of making the spreadsheet more interactive and provides obvious actions that are available to the user of the spreadsheet. Figure 14-1 shows two Windows Forms buttons that have been placed on a spreadsheetone that refreshes the stock quotes and the other that trades a particular stock.
Figure 14-1. Two Windows Forms controls on a spreadsheet.
[View full size image]

Figure 14-2. DateTimePicker controls on a spreadsheet.
Chapter 15 discusses the actions pane.
Figure 14-3. Using the DateTimePicker control in the Document Actions task pane.
[View full size image]

When to Use a Modal or Modeless Windows Forms Form
Another way to use Windows Forms in an Office application is to use a standard Windows Forms form shown as a dialog. For example, you could handle the BeforeDoubleClick event for the worksheet and if a cell containing a date is double-clicked, you could display a custom Windows Forms form, as shown in Figure 14-4.
Figure 14-4. Displaying a Windows Forms dialog when the user double-clicks a cell.
[View full size image]

Listing 14-1. A VSTO Excel Customization That Displays a Modeless Form
Note that using the ActionsPane feature of VSTO is often an easier way to achieve a modeless result because it provides all the benefits of a modeless form with the addition of the ability to dock within the Office window space.
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
{
public Button btn1;
public Form form1;
private void Sheet1_Startup(object sender, EventArgs e)
{
MessageBox.Show(System.Threading.Thread.ManagedThreadID);
btn1 = new Button();
btn1.Click += new EventHandler(btn1_Click);
form1 = new Form();
form1.Controls.Add(btn1);
form1.Show();
Globals.ThisWorkbook.BeforeClose +=
new Excel.WorkbookEvents_BeforeCloseEventHandler(
ThisWorkbook_BeforeClose);
}
void btn1_Click(object sender, EventArgs e)
{
MessageBox.Show(System.Threading.Thread.ManagedThreadID);
}
void ThisWorkbook_BeforeClose(ref bool Cancel)
{
form1.Close();
}
#region VSTO Designer generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(this.Sheet1_Startup);
}
#endregion
}
}