Condividi tramite


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

This walkthrough demonstrates how to use buttons and text boxes in a document-level customization for Microsoft Office Word.

Applies to: The information in this topic applies to document-level projects for Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Adding controls to the Word document in a document-level project at design time.

  • Populating a text box when a button is clicked.

Note

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 Customizing Development Settings in Visual Studio.

Prerequisites

You need the following components to complete this walkthrough:

Creating the Project

The first step is to create a Word Document project.

To create a new project

  • Create a Word Document project with the name My Word Button. In the wizard, select Create a new document.

    For more information, see How to: Create Office Projects in Visual Studio.

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

Adding Controls to the Word Document

The user interface controls consist of a button and a text box on the Word document.

To add a button and a text box

  1. Verify that the document is open in the Visual Studio designer.

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

    Note

    In Word, controls are dropped in-line with text by default. You can modify the way controls and shape objects are inserted by changing the default on the Edit tab of the Options dialog box in Word.

  3. On the View menu, click Properties Window.

  4. Find TextBox1 in the Properties window drop-down box and change the Name property of the text box to displayText.

  5. Drag a Button control to the document and change the following properties.

    Property

    Value

    Name

    insertText

    Text

    Insert Text

Now you can write the code that will run when the button is clicked.

Populating the Text Box When the Button Is Clicked

Every time the user clicks the button, Hello World! is added to the text box.

To write to the text box when the button is clicked

  1. In Solution Explorer, right-click ThisDocument, 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 for the button to the Startup event. For information about creating event handlers, see How to: Create Event Handlers in Office Projects.

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

Testing the Application

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

To test your document

  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 Word documents. Here are some tasks that might come next:

See Also

Tasks

How to: Add Windows Forms Controls to Office Documents

Concepts

Windows Forms Controls on Office Documents Overview

Walkthroughs Using Word

Host Items and Host Controls Overview

Other Resources

Office Development Samples and Walkthroughs