Walkthrough: Displaying Text in a Text Box in a Worksheet Using a Button

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

This walkthrough shows the basics of using buttons and text boxes on Microsoft Office Excel worksheets, and how to create Excel projects using Visual Studio Tools for Office. To see the result as a completed sample, see Excel Controls Sample.

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.


Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.


To complete this walkthrough, you will need:

  • Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).

  • Microsoft Office Excel 2003, or Microsoft Office Excel 2007.

Creating the Project

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 How to: Create Visual Studio Tools for Office Projects.

    Visual Studio opens the new Excel workbook in the designer and adds the My Excel Button project to Solution Explorer.

Adding Controls to the Worksheet

For this walkthrough, you will need a button and a text box on the first worksheet.

To add a button and a text box

  1. Verify that the My Excel Button.xls workbook is open in the Visual Studio designer, with Sheet1 displayed.

  2. From the Common Controls tab of the Toolbox, drag a TextBox to Sheet1.

  3. From the View menu, select Properties Window.

  4. Be sure that TextBox1 is visible in the Properties window drop-down box and change the Name property of the text box to displayText.

  5. Drag a Button control onto Sheet1 and change the following properties:






    Insert Text

Now write the code to run when the button is clicked.

Populating the Text Box 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

  1. In Solution Explorer, right-click Sheet1, and then click View Code on the shortcut menu.

  2. Add the following code to the Click event handler of the button:

    Private Sub insertText_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles insertText.Click
        Me.displayText.Text += "Hello World! " 
    End Sub
    private void insertText_Click(object sender, EventArgs e)
        this.displayText.Text += "Hello World! ";
  3. In C#, you must add an event handler to the Startup event as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    this.insertText.Click += new EventHandler(insertText_Click);

Testing the Application

You can now test your workbook to make sure that the message Hello World! appears in the text box when you click the button.

To test your workbook

  1. Press F5 to run your project.

  2. Click the button.

  3. Confirm that Hello World! appears in the text box.

Next Steps

This walkthrough shows the basics of using buttons and text boxes on Excel worksheets. Here are some tasks that might come next:

See Also


How to: Add Windows Forms Controls to Office Documents


Walkthroughs Using Excel

Limitations of Windows Forms Controls on Office Documents