Walkthrough: Displaying Text in a Text Box in a Worksheet Using a Button
This walkthrough shows the basics of using buttons and text boxes on Microsoft Office Excel worksheets, and how to create Excel projects using Office development tools in Visual Studio. To see the result as a completed sample, see the Excel Controls Sample at Office Development Samples and Walkthroughs.
Applies to: The information in this topic applies to document-level projects for Excel. For more information, see Features Available by Office Application and Project Type.
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.
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Excel 2013 or Excel 2010.
In this step, you will create an Excel Workbook project using Visual Studio.
Create an Excel Workbook project with the name My Excel Button. Make sure that Create a new document is selected. For more information, see How to: Create Office Projects in Visual Studio.
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.
Verify that the My Excel Button.xlsx workbook is open in the Visual Studio designer, with
From the Common Controls tab of the Toolbox, drag a TextBox to
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:
Property Value Name insertText Text Insert Text
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.
In Solution Explorer, right-click Sheet1, and then click View Code on the shortcut menu.
Add the following code to the Click event handler of the button:
You can now test your workbook to make sure that the message Hello World! appears in the text box when you click the button.
Press F5 to run your project.
Click the button.
Confirm that Hello World! appears in the text box.
This walkthrough shows the basics of using buttons and text boxes on Excel worksheets. Here are some tasks that might come next:
Deploying the project. For more information, see Deploying an Office Solution.
Using check boxes to change formatting. For more information, see Walkthrough: Changing Worksheet Formatting Using CheckBox Controls.