Adding Other Controls to the Ribbon

So far you've seen examples that show how to create your own toggle button or use an existing one. You can add a number of other controls to the Ribbon, and you can see a list of them in IntelliSense, as shown in Figure 14.23.

Figure 14.22. Custom button added to custom group within built-in tab

Ribbonl.Hml* Ribbonl .vb [" ThisApplication.vb [ File System (ExcelAddinlSetup) [ Start Page |

B <customUI xralns='rhttp : / /schémas .microsoft. com/of fice/200 6/01/custoraui" onLoad="OnLoad"> <ribbon startFroitiScratch="false"> <tabs>

<tab id="VSTO.Tab" label=".NET Add-in Tab" visible="1">

<group id="VSTO.Group" label= "Hello World Group" visible="l"> <toggleButton id="toggleButton" la£iel= "Hello World"

screentip="Hello World Screentip" onAction="OnToggleButton" />

ED box ED button </ ta iID buttonGroup </ c ibb ED checkBox </custom ED comboBox ED control ED dropDown ED dynamicMenu ED editBox ED gallery ED labelControl ED menu ED separator ED splitButton ^J

Figure 14.23. Controls displayed in IntelliSense

In this section, you'll look at some of these controls and learn the event handler signature for each control. We cover only the basic controls: button, check box, combo box, gallery, menu, and split button. You'll create event handlers for each of the buttons, but for the graphics you'll reference existing controls. You'll create the first three controls for Excel.


The Button control is similar to toggleButton, except that Button doesn't have an on and off state; users click the button, and the code in the event handler identified in the onAction attribute runs. Here's the XML markup needed to add a button to the Ribbon.

<button id="myButton" label="Printer Setup"

imageMso="FilePrint" screentip="set up the printer" onAction="OnClick" />

The code in Listing 14.9 shows the event handler to be written in the Ribbon Callbacks region of the Button1.vb class. When a user clicks the button, the Printer Setup dialog box appears.

Listing 14.9. Adding a Button control

Public Sub OnClick(ByVal control As Office.IRibbonControl) Globals.ThisAddin.Application.Dialogs( _


End Sub CheckBox

Check box controls enable a Boolean state when users select or deselect the control. Here's the XML markup needed to add a check box to the Ribbon.

<checkBox id="hideWorksheet" label ="Hide Sheet1" onAction="OnCheck"/>

The code in Listing 14.10 shows the event handler that hides or shows Sheet1 according to the state of the check box.

Listing 14.10. Adding a check box control

Public Sub OnCheck(ByVal control As Office.IRibbonControl, _ ByVal isChecked As Boolean)

Dim Sheet1 As Excel.Worksheet = _

Globals.ThisAddin.Application.Sheets(1) If isChecked Then

Sheet1.Visible = Excel.XlSheetVisibility.xlSheetHidden


Sheet1.Visible = Excel.XlSheetVisibility.xlSheetVisible End If

End Sub ComboBox

0 0

Post a comment