Adding Controls to Excel and Word Documents

In the same way that you add ActiveX controls to a Word document or Excel worksheet and you program against the controls using VBA, you can add Windows Forms controls to your document (or worksheet) with VSTO. To add controls to the document, you drag them from the Toolbox to an area on your document. The controls can be placed anywhere on the document surface except within a header or footer.

You can add controls to your document at design time or at run time. At design time, the document (or worksheet) surface is the designer. Just as you add controls to a UserForm or Windows Form, you drag controls from the Toolbox and drop them directly onto the document designer. To view the Windows Forms controls that can be added to a document at design time, click Toolbox on the View menu in Visual Studio. Figure 8.15 shows the controls available to an Excel worksheet, along with a number of controls that have been added to the worksheet.

The Toolbox contains tabs for various types of controls, such as Common Controls, Components, Excel Controls, and All Windows Forms. This

Toolbox -w 1?

X

ControlEKample.Kls* Object Browser Start Page

T X

B All Windows Forms 0 Common Controls

Pointer ® Button 0 CheckBox H2 CheckedListBox

I Arial - 10 T B 7 IT jM^jS « * • » " »

A cli 1 ^ aa, 1 * ¿i a ■ j 1 - « 1 i, £ - îj i\ 1 m 4} - d

B1B - f.

A

R

c

n

E

F

G

H

J

v.

1

1

IS April. 2006 13

|l=| LomboBox

y,

Date I imeHcker A LinkLabel A Label E3 ListBox List View 1*^1 MaskedTextBox §f MonthCalendar im NumericUpDoiAin Kal PictureBox 5ID ProgressBar ® RadioButton @ TextBox l^-i ToolTip TreeView WebBrowser

4

5

6

12 13 14 15

19 20 21 22 26 27 28 29 3 4 5 S 4/16/200E

7

8

30 1

9

10

Today

Bullonl I

11

1?

O R«neu

13

14

O RfidinBul!an2

15

16

17

18

1

1

13

20

21

0 Components

22

23

0 Exc'eHlantrols

24

0 General

25

There are no usable controls in this group, Drag an item onto this text to add it to the toolbox,

26

27

v

28

* Sil

RRtl / -!l

Rl? i -Il i

3/

<

■i

>

Figure 8.15. The Toolbox in Visual Studio

Figure 8.15. The Toolbox in Visual Studio tab design helps you display only those controls that you are interested in instead of having to scroll through all the available Windows Forms controls (and host controls).

Host controls are another type of control that can be added to the document. These controls are described in detail in Chapters 6 and 7; therefore, this chapter focuses on Windows Forms controls that can be added to the document or worksheet.

0 0

Post a comment