Display Windows Forms

Excel projects can use Windows forms to gather information and display results. Windows forms are a huge improvement over the forms you can display from Excel VBA.

How to do it

To create a Windows form in Visual Studio .NET for use from Excel, follow these steps:

  1. From the Project menu, choose Add Windows Form. Visual Studio .NET displays the Add New Item dialog box.

  2. Enter a name for the form and click OK. Visual Studio .NET creates a new Windows form class and displays the class in the Designer.

  3. Use the Designer to add controls to the form; then switch to the Code window. Unlike previous versions of Visual Basic, VB.NET describes the entire form in terms of code. The form and control properties are all maintained in the “Windows Form Designer generated code” region of the form’s class.

  4. In order to enable the form to interact with Excel, add the following lines to the generated code (shown in bold ):

    	                    Imports Excel = Microsoft.Office.Interop.Excel
        Public Class SimpleForm
            Inherits System.Windows.Forms.Form
    Dim xlCode As OfficeCodeBehind
        #Region " Windows Form Designer generated code "
            Public Sub New(ByVal target As OfficeCodeBehind)
                MyBase.New(  )
                'This call is required by the Windows Form Designer.
                InitializeComponent(  )
                'Add any initialization after the InitializeComponent(  ) call
                ' Get the OfficeCodeBehind class that created this form
    	                    ' (used to return responses to Excel).
    	                    xlCode = target
            End Sub
            ' Remainder of class omitted here...
  5. Within the form’s ...

Get Excel 2003 Programming: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.