Walkthrough: Creating a Custom Tab by Using the Ribbon Designer
This walkthrough demonstrates how to create a custom Ribbon tab by using the Ribbon Designer. You can use the Ribbon Designer to add and position controls on the custom tab.
Applies to: The information in this topic applies to document-level projects and application-level projects for the following applications: Excel 2007 and Excel 2010; InfoPath 2010; Outlook 2007 and Outlook 2010; PowerPoint 2007 and PowerPoint 2010; Project 2010; Visio 2010; Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.
This walkthrough illustrates the following tasks:
Creating actions panes.
Creating a custom tab.
Hiding and showing actions panes by using buttons on the custom tab.
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.
For a video version of this topic, see Video How to: Creating a Custom Tab by Using the Ribbon Designer. For a related video demonstration, see How Do I: Use the Ribbon Designer to Customize the Ribbon in Excel?.
The steps for using the Ribbon Designer are almost identical for all Office applications. This example uses an Excel workbook.
To create an Excel workbook project
Create an Excel workbook project with the name MyExcelRibbon. For more information, see How to: Create Office Projects in Visual Studio.
Visual Studio opens the new workbook in the designer and adds the MyExcelRibbon project to Solution Explorer.
Add two custom actions panes to the project. You will later add buttons to the custom tab that show and hide these actions panes.
To create actions panes
On the Project menu, click Add New Item.
In the Add New Item dialog box, select ActionsPaneControl, and then click Add.
The ActionsPaneControl1.cs or ActionsPaneControl1.vb file opens in the designer.
From the Common Controls tab of the Toolbox, add a label to the designer surface.
In the Properties window, set the Text property of label1 to Actions Pane 1.
Repeat steps 1 through 5 to create a second actions pane and label. Set the Text property of the second label to Actions Pane 2.
One of the Office application design guidelines is that users should always have control of the Office application UI. To add this capability for the actions panes, you can add buttons that show and hide each actions pane from a custom tab on the Ribbon. To create a custom tab, add a Ribbon (Visual Designer) item to the project. The designer helps you add and position controls, set control properties, and handle control events.
To create a custom tab
On the Project menu, click Add New Item.
In the Add New Item dialog box, select Ribbon (Visual Designer).
Change the name of the new Ribbon to MyRibbon, and click Add.
The MyRibbon.cs or MyRibbon.vb file opens in the Ribbon Designer and displays a default tab and group.
In the Ribbon Designer, click the default tab.
In the Properties window, expand the ControlId property, and then set the ControlIdType property to Custom.
Set the ControlId property to My Custom Tab.
In the Ribbon Designer, click group1.
In the Properties window, set Label to Actions Pane Manager.
From the Office Ribbon Controls tab of the Toolbox, drag a button onto group1.
Click button1 to select it.
In the Properties window, set Label to Show Actions Pane 1.
Add a second button to group1, and set the Label property to Show Actions Pane 2.
From the Office Ribbon Controls tab of the Toolbox, drag a ToggleButton control onto group1.
Set the Label property to Hide Actions Pane.
The last step is to add code that responds to the user. Add event handlers for the Click events of the two buttons and the Click event of the toggle button. Add code to these event handlers that hide and show the actions panes.
To hide and show actions panes by using buttons in the custom tab
In Solution Explorer, right-click MyRibbon.cs or MyRibbon.vb, and then click View Code.
Add the following code to the top of the MyRibbon class. This code creates two actions pane objects.
Replace the MyRibbon_Load method with the following code. This code adds the actions pane objects to the ActionsPane.Controls collection and hides the objects from view. The Visual C# code also attaches delegates to several Ribbon control events.
Private Sub MyRibbon_Load(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs) Handles MyBase.Load Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1) Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane2) actionsPane1.Hide() actionsPane2.Hide() Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False End Sub
For more information about the differences in the Visual C# code example for projects that target the .NET Framework 4 and the .NET Framework 3.5, see Updating Ribbon Customizations in Office Projects that You Migrate to the .NET Framework 4.
Add the following three event handler methods to the MyRibbon class. These methods handle the Click events of the two buttons and the Click event of the toggle button. The event handlers for button1 and button2 show alternate actions panes. The event handler for toggleButton1 shows and hides the active actions pane.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _ Handles Button1.Click Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True actionsPane2.Hide() actionsPane1.Show() ToggleButton1.Checked = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _ Handles Button2.Click Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True actionsPane1.Hide() actionsPane2.Show() ToggleButton1.Checked = False End Sub Private Sub ToggleButton1_Click(ByVal sender As System.Object, _ ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _ Handles ToggleButton1.Click If ToggleButton1.Checked Then Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False Else Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True End If End Sub
When you run the project, Excel starts, The My Custom Tab tab appears on the Ribbon. Click the buttons on My Custom Tab to show and hide the actions panes.
To test the custom tab
Press F5 to run your project.
Click the My Custom Tab tab.
In the Custom Actions Pane Manager group, click Show Actions Pane 1.
The actions pane appears and displays the label Actions Pane 1.
Click Show Actions Pane 2.
The actions pane appears and displays the label Actions Pane 2.
Click Hide Actions Pane.
The actions panes are no longer visible.
You can learn more about how to customize the Office UI from these topics: