Walkthrough: Displaying Text in a Text Box in a Worksheet Using a Button
The features in this topic are available only if you have the required applications installed.
For more information, see.
This walkthrough shows the basics of using buttons and text boxes on Microsoft Office Excel 2003 worksheets, and how to create Excel projects using Microsoft Visual Studio 2005 Tools for the Microsoft Office System. To see the result as a completed sample, see.
During this walkthrough, you will learn how to:
Add controls to a worksheet.
Populate a text box when a button is clicked.
Test your project.
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 Workbook project using Visual Studio Tools for Office.
To create a new project
Create an Excel Workbook project with the name My Excel Button. 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 My Excel Button project to Solution Explorer.
For this walkthrough, you will need a button and a text box on the first worksheet.
To add a button and a text box
Verify that the My Excel Button.xls workbook is open in the Visual Studio designer, with
From the Common Controls tab of the Toolbox, drag ato
From the View menu, select Properties Window.
Be sure that TextBox1 is visible in the Properties window drop-down box and change the Name property of the text box to displayText.
Drag a Button control onto
Sheet1and change the following properties:
Now write the code to run when the button is clicked.
Each time the user clicks the button, Hello World! is appended to the text box.
To write to the text box when the button is clicked
In Solution Explorer, right-click Sheet1, and then click View Code on the shortcut menu.
Add the following code to theevent handler of the button:
In C#, you must add an event handler to theevent as shown below. For information on creating event handlers, see .