Adding Custom Controls to a Document

At times you'll want to use a third-party control or create your own custom control for use on a document. One way to do this is to create a user control, add some Windows Forms controls to it, and then add the user control to the document.

For example, if you want to make radio buttons mutually exclusive when used on a document, you must use a GroupBox. Unfortunately, GroupBox controls are not supported for use on a document at design time. Instead, you can add the GroupBox control to a user control and then add controls such as RadioButton controls. When you build your project, the user control will be added to the Toolbox. After the user control appears on the Toolbox, you can drag it directly to your document, thus giving you the functionality of the group box. Let's try this on an Excel workbook.

1. Create an Excel workbook project using VSTO.

2. Right-click the Excel solution, point to Add, and then click User Control.

❖ Note The UI for creating projects might differ depending on the profile you selected when you installed Visual Studio.

3. Keep the default name UserControl1, and click Add.

4. Drag a GroupBox control to the user control, and change the Text property to Mutually Exclusive.

5. Drag three RadioButton controls to the GroupBox control, leaving the default names RadioButton1, RadioButton2, and RadioButton3.

6. Build the solution by clicking Build Solution on the Build menu.

7. Right-click Sheet1, and click View Designer.

The control is added to the Toolbox in the GroupBox Components tab, as shown in Figure 8.26.

UserCortrdl ,vb [Design]

- 'GffonipBow Components Ht Pohter & U set Contrail ± All Window* Farm» + Common Controls ± Oat-a + Components > Diahog» t EHtcl Controls

There -are no usable controls In this croup. Drag -an tern cnto this text to add it to the toolbox,

GtoupBoK.Nb Etart Page Obiect - B J U = = ~ jj j ^ j. ?a * a - y I

UserCortrdl ,vb [Design]

GtoupBoK.Nb Etart Page Obiect - B J U = = ~ jj j ^ j. ?a * a - y I













Figure 8.26. Adding a user control to the Toolbox

8. Drag UserControl1 to cell A1 of Sheet1.

9. Drag a RadioButton from the Toolbox to cell A9 (or to a cell below UserControl1) on Sheet1, and then add two more RadioButton controls beneath it.

10. Press F5 to run the code.

11. In the Mutually Exclusive GroupBox, click RadioButton1, then RadioButton2, and then RadioButton3.

12. For the controls you added directly to the worksheet, click RadioButton1, then RadioButton2, and then RadioButton3.

Because RadioButton controls that are directly added to the document are not automatically mutually exclusive, all three controls appear to be selected, as shown in Figure 8.27. The RadioButtons that were placed in a GroupBox on a custom user control are mutually exclusive, and only RadioButton3 appears to be selected.

C Microsoft Excel! - GroupBox.xis

iäj pie Edit view Insert Format

l«lt Ddtd

V/ÏM}J" hep

Arial Ä110

II ï =

i J ._! A jj.ij

A -Ü -à

- j î- a

m - s

A 1 B 1 C




1 M lAidL £ Hcluswe

i 3 1

ö Racicöuitiinl

O' Raiceuttiinî


© R*ioButton3





0 RadnBiilonl


C*) RwiinRiJtlrv)7


© Rad»Buitw3







Figure 8.27. Selecting mutually exclusive RadioButton controls

Figure 8.27. Selecting mutually exclusive RadioButton controls

0 0

Post a comment