
Display a Windows Form from Excel 2003 #85
Chapter 11, Visual Studio Tools for Office
|
377
HACK
you created for this class. This should be done after the call to Initialize-
Component
(which initializes the form):
public GetCustomer (OfficeCodeBehind targetExcelCode)
{
InitializeComponent( );
this.excelCode = targetExcelCode;
}
The next step is to create an instance of the Windows Forms class and call
the
Show method. In this example, you are going to do this when the work-
book first opens the new document:
protected void ThisWorkbook_Open( )
{
GetCustomer inputForm = new GetCustomer (this);
inputForm.Show( );
}
At this point, you should be able to build and test your code. Excel should
start and open your blank form. Sure, it doesn’t do much yet, but you
should run it now to make sure that all of the pieces are falling together.
Gather Data with the Windows Forms
Now that you have the form opening up when the user starts a new instance
of the Excel document, you need to add code to the form’s Load event to
populate the combo boxes with data from a database. First, to make life a
little easier, in your Windows Forms class, you need to add references to the
data namespaces listed here:
using System.Data;
using System.Data.SqlClient;
Next, create a method to handle the form’s Load event. The easiest way to
do this is to double-click anywhere on the form (as long as it is not a con-
trol), and you’ll be placed ...