Walkthrough: Changing Worksheet Formatting Using CheckBox Controls
This walkthrough shows the basics of using check boxes on a Microsoft Office Excel worksheet to change formatting. You will use Office development tools in Visual Studio to create and add code to your project. 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 2007 and Excel 2010. For more information, see Features Available by Office Application and Project Type.
During this walkthrough, you will learn how to:
Add text and controls to a worksheet.
Format the text when an option is selected.
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 Working with Settings.
You need the following components to complete this walkthrough:
An edition of Visual Studio 2010 that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Microsoft Office Excel 2007 or Excel 2010.
In this step, you will create an Excel Workbook project by using Visual Studio.
To create a new project
Create an Excel Workbook project with the name My Excel Formatting. 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 Formatting project to Solution Explorer.
To add three check boxes
Verify that the workbook is open in the Visual Studio designer and that Sheet1 is open.
From the Common Controls tab of the Toolbox, drag a CheckBox control to or near cell B2 in Sheet1.
From the View menu, select Properties window.
Be sure that Checkbox1 is visible in the object name list box of the Properties window, and change the following properties:
Drag a second check box on or near cell B4 and change the following properties:
Drag a third check box on or near cell B6 and change the following properties:
Select all three check box controls while holding the CTRL key.
On the Excel toolbar, click Align Lefts and then click Make Vertical Spacing Equal.
The three check box controls now have equal vertical spacing and are aligned on the left side, at the position of the first control you selected.
Next, you will drag a NamedRange control to the worksheet.
You can also add the NamedRange control by typing textFont into the Name box.
To add text to a NamedRange control
From the Excel Controls tab of the toolbox, drag a NamedRange control to cell B9.
Verify that $B$9 appears in the editable text box, and that cell B9 is selected. If it is not, click cell B9 to select it.
Cell B9 becomes a range named NamedRange1.
There is no visible indication on the worksheet, but NamedRange1 appears in the Name box (just above the worksheet on the left side) when cell B9 is selected.
Be sure that NamedRange1 is visible in the object name list box of the Properties window, and change the following properties:
Click a check box to change the formatting of this text.
Next, write the code to format the text when an option is selected.
In this section, you will write code so that when the user selects a formatting option, the format of the text in the worksheet is changed.
To change formatting when a check box is selected
Right-click Sheet1, and then click View Code on the shortcut menu.
Add the following code to the Click event handler of the applyBoldFont check box:
Add the following code to the Click event handler of the applyItalicFont check box:
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.textFont.Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleSingle Else Me.textFont.Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleNone End If End Sub
This walkthrough shows the basics of using check boxes and formatting text on Excel worksheets. Here are some tasks that might come next:
Deploying the project. For more information, see Deployment Overview (Office Development in Visual Studio).
Using a button to populate a text box. For more information, see Walkthrough: Displaying Text in a Text Box in a Worksheet Using a Button.