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 Personalize the IDE.
Prerequisites
You need the following components to complete this walkthrough:
Visual Studio opens the new Word document in the designer and adds the My Word Formatting project to Solution Explorer.
Add text and controls to the Word document
For this walkthrough, add three check boxes and some text in a Bookmark control to the Word document. The check boxes will present options to the user for formatting the text.
Add three check boxes
Verify that the document is open in the Visual Studio designer.
From the Common Controls tab of the Toolbox, drag the first CheckBox control to the document.
In the Properties window, change the following properties.
Property
Value
Name
applyBoldFont
Text
Bold
Press Enter to move the insertion point below the first check box.
Add a second check box to the document below the ApplyBoldFont check box and change the following properties.
Property
Value
Name
applyItalicFont
Text
Italic
Press Enter to move the insertion point below the second check box.
Add a third check box to the document below the ApplyItalicFont check box and change the following properties.
Property
Value
Name
applyUnderlineFont
Text
Underline
Add text and a Bookmark control
Move the insertion point below the check box controls and type the following text:
Click a check box to change the formatting of this text.
From the Word Controls tab of the Toolbox, drag a Bookmark control to the document.
The Add Bookmark Control dialog box appears.
Select the text you added to the document and click OK.
A Bookmark control named Bookmark1 is added to the selected text in the document.
In the Properties window, change the value of the (Name) property to fontText.
Next, write the code to format the text when a check box is checked or cleared.
Format the text when a check box is checked or cleared
When the user selects a formatting option, change the format of the text in the document.
Change formatting when a check box is selected
Right-click ThisDocument in Solution Explorer, and then click View Code on the shortcut menu.
For C# only, add the following constants to the ThisDocument class.
const int WordTrue = -1;
const int WordFalse = 0;
Add the following code to the Click event handler of the applyBoldFont check box.
Private Sub applyBoldFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles applyBoldFont.Click
Me.fontText.Bold = Me.applyBoldFont.Checked
End Sub
Add the following code to the Click event handler of the applyItalicFont check box.
Private Sub applyItalicFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles applyItalicFont.Click
Me.fontText.Italic = Me.applyItalicFont.Checked
End Sub
Add the following code to the Click event handler of the applyUnderlineFont check box.
Private Sub applyUnderlineFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles applyUnderlineFont.Click
If Me.applyUnderlineFont.Checked Then
Me.fontText.Underline = Word.WdUnderline.wdUnderlineSingle
Else
Me.fontText.Underline = Word.WdUnderline.wdUnderlineNone
End If
End Sub
this.applyBoldFont.Click += new EventHandler(applyBoldFont_Click);
this.applyItalicFont.Click += new EventHandler(applyItalicFont_Click);
this.applyUnderlineFont.Click += new EventHandler(applyUnderlineFont_Click);
Test the application
You can now test your document to verify that the text is formatted correctly when you select or clear a check box.
Test your document
Press F5 to run your project.
Select or clear a check box.
Confirm that the text is formatted correctly.
Next steps
This walkthrough shows the basics of using check boxes and programmatically changing text formatting on Word documents. Here are some tasks that might come next: