Display Forms
Excel projects can use Windows forms to gather information and display results. To create a Windows form in Visual Studio .NET for use from Excel, follow these steps:
From the Project menu, choose Add Windows Form. Visual Studio .NET displays the Add New Item dialog box.
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.
Use the Designer to add controls to the form; then switch to the Code window. Unlike previous versions of Visual Basic, Visual Basic .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.
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...Within the form’s event procedures, use the
xlCodeobject created in Step 4 to interact with Excel. For example, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access