
376
|
Chapter 11, Visual Studio Tools for Office
#85 Display a Windows Form from Excel 2003
HACK
Once the Visual Studio Wizard has completed the project setup, you will be
dropped into the solution. At this point, you will need to add a Windows
Form to the application and name it
GetCustomer.
Next, create a form that looks similar to Figure 11-6 and set the controls’
properties according to Table 11-1.
Initialize the Windows Form
In order to communicate with the code behind the Excel document, you will
need to pass the
ThisWorkbook object as a parameter to your Windows Form.
So, first, you will need to create a class-level variable in your Windows
Form:
private OfficeCodeBehind excelCode;
The next step is to modify the Windows Forms constructor to accept the
ThisWorkbook object as a parameter. In C#, the form constructor will have
the same name as your form class. Here’s the empty constructor for the
GetConstructor form:
public GetCustomer( )
{
}
Update this constructor to take the CodeBehind object as a parameter of type
OfficeCodeBehind and add code to store the object in the excelCode variable
Figure 11-6. GetCustomer Windows Form
Table 11-1. Windows Form controls and properties
Required controls Properties
Label Text: Customer Name:
ComboBox Name: cboCustomer
Label Text: Year
ComboBox Name: cboYear
Button (Get Data) Name: cmdOK