Walkthrough: Displaying Text in a Text Box in a Document Using a Button
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
This walkthrough demonstrates how to use buttons and text boxes in a document-level customization for Microsoft Office Word.
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.
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.
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 Visual Studio Tools for Office Projects.
Visual Studio opens the new Word document in the designer and adds the My Word Button project to Solution Explorer.
The user interface controls consist of a button and a text box on the Word document.
To add a button and a text box
Verify that the document is open in the Visual Studio designer.
From the Common Controls tab of the Toolbox, drag a TextBox control to the document.
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.
On the View menu, click Properties Window.
Find TextBox1 in the Properties window drop-down box and change the Name property of the text box to displayText.
Drag a Button control to the document and change the following properties.
Now you can write the code that will run 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
In Solution Explorer, right-click ThisDocument, and then click View Code on the shortcut menu.
Add the following code to the Click event handler of the button.
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 Visual Studio Tools for Office.
This walkthrough shows the basics of using buttons and text boxes on Word documents. Here are some tasks that might come next:
Using a combo box to change formatting. For more information, see Walkthrough: Changing Document Formatting Using CheckBox Controls.
Using radio buttons to select chart styles. For more information, see Walkthrough: Updating a Chart in a Document Using Radio Buttons.