Walkthrough: Collecting Data Using a Windows Form
Some code examples in this topic use the this or Me keyword or the
This walkthrough demonstrates how to open a Windows Form from a Microsoft Office Excel 2003 worksheet, collect information from the user, and write that information into a worksheet cell.
To complete this walkthrough, you will need:
Microsoft Visual Studio 2005 Tools for the Microsoft Office System.
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see.
In this step, you will create an Excel Application project.
To create a new project
Create an Excel Workbook project with the name WinFormInput. Make sure that Create a new document is selected. For more information, see.
Visual Studio opens the new Excel workbook in the designer and adds the WinFormInput project to Solution Explorer.
To add a named range to Sheet1
Select cell A1 on
In the Name box, type formInput.
The Name box is located to the left of the formula bar, just above column A of the worksheet.
Acontrol is added to cell A1. There is no visible indication on the worksheet, but formInput appears in the Name box (just above the worksheet on the left side) and in the Properties window when cell A1 is selected.
Create a Windows Form to prompt the user for information.
To add a Windows Form
Select the project WinFormInput in Solution Explorer.
On the Project menu, click Add Windows Form.
Name the form GetInputString.vb or GetInputString.cs and then click Add.
The new form opens in the designer.
Add aand a to the form.
Select the button, find the property Text in the Properties window, and change the text to OK.
Next, add code to
ThisWorkbook.cs to collect the user's information.
Create an instance of the
GetInputString Windows Form and display it, and then write the user's information into a cell in the worksheet.
To display the form and collect information
Right-click ThisWorkbook.vb or ThisWorkbook.cs in Solution Explorer and select View Code.
In theevent handler of
ThisWorkbook, add the following code to declare a variable for the form
GetInputStringand then show the form:
In C#, you must add an event handler as shown in theevent below. For information on creating event handlers, see .
Create a method called
WriteStringToCellthat writes text to a named range. This method is called from the form, and the user's input will be passed to the NamedRange control,
formInput, on cell A1.
Next add code to the form to handle the button's click event.
To send information to the worksheet
Right-click GetInputString in Solution Explorer, and select View Designer.
Double-click the button to open the code file with the button'sevent handler added.
Add code to the event handler to take the input from the text box, send it to the function
WriteStringToCell, and then close the form.
You can now test your workbook to make sure that the Windows Form appears, and that your input appears in the worksheet.
To test your workbook
Press F5 to run your project.
Confirm that the Windows Form appears.
Type Hello World in the text box and click OK.
Confirm that Hello World appears in cell A1 of the worksheet.