Sdílet prostřednictvím


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.

Poznámka

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.

Prerequisites

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](bb398242\(v=vs.100\).md).
  • Microsoft Office Excel 2007 or Excel 2010. 

link to video 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?.

Creating an Excel Workbook Project

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.

Creating Actions Panes

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

  1. On the Project menu, click Add New Item.

  2. In the Add New Item dialog box, select ActionsPaneControl, and then click Add.

    The ActionsPaneControl1.cs or ActionsPaneControl1.vb file opens in the designer.

  3. From the Common Controls tab of the Toolbox, add a label to the designer surface.

  4. In the Properties window, set the Text property of label1 to Actions Pane 1.

  5. 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.

Creating a Custom Tab

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

  1. On the Project menu, click Add New Item.

  2. In the Add New Item dialog box, select Ribbon (Visual Designer).

  3. 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.

  4. In the Ribbon Designer, click group1.

  5. In the Properties window, set Label to Actions Pane Manager.

  6. From the Office Ribbon Controls tab of the Toolbox, drag a button onto group1.

  7. Click button1 to select it.

  8. In the Properties window, set Label to Show Actions Pane 1.

  9. Add a second button to group1, and set the Label property to Show Actions Pane 2.

  10. From the Office Ribbon Controls tab of the Toolbox, drag a ToggleButton control onto group1.

  11. Set the Label property to Hide Actions Pane.

Hiding and Showing Actions Panes by Using Buttons on the Custom Tab

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

  1. In Solution Explorer, right-click MyRibbon.cs or MyRibbon.vb, and then click View Code.

  2. Add the following code to the top of the MyRibbon class. This code creates two actions pane objects.

    Dim actionsPane1 As New ActionsPaneControl1()
    Dim actionsPane2 As New ActionsPaneControl2()
    
    ActionsPaneControl1 actionsPane1 = new ActionsPaneControl1();
    ActionsPaneControl2 actionsPane2 = new ActionsPaneControl2();
    
  3. 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 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
    
    private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
    {
        Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1);
        Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane2);
        actionsPane1.Hide();
        actionsPane2.Hide();
        Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = false;
    
        // Use the following code in projects that target the .NET Framework 4.
        this.button1.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(
            this.button1_Click);
        this.button2.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(
            this.button2_Click);
        this.toggleButton1.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(
            this.toggleButton1_Click);
    
        // For .NET Framework 3.5 projects, use the following code instead.
        // this.button1.Click += new EventHandler<RibbonControlEventArgs>(button1_Click);
        // this.button2.Click += new EventHandler<RibbonControlEventArgs>(button2_Click);
        // this.toggleButton1.Click += new EventHandler<RibbonControlEventArgs>(toggleButton1_Click);
    }
    

    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.

  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
    
    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = true;
        actionsPane2.Hide();
        actionsPane1.Show();
        ToggleButton1.Checked = false;
    }
    
    private void button2_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = true;
        actionsPane1.Hide();
        actionsPane2.Show();
        ToggleButton1.Checked = false;
    
    }
    
    private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
    {
        if (toggleButton1.Checked == true)
        {
            Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = false;
        }
        else
        {
            Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = true;
        }
    
    }
    

Testing the Custom Tab

When you run the project, Excel starts, The Add-Ins tab appears on the Ribbon. Click the buttons on Add-Ins to show and hide the actions panes.

To test the custom tab

  1. Press F5 to run your project.

  2. Click the Add-Ins tab.

  3. In the Custom Actions Pane Manager group, click Show Actions Pane 1.

    The actions pane appears and displays the label Actions Pane 1.

  4. Click Show Actions Pane 2.

    The actions pane appears and displays the label Actions Pane 2.

  5. Click Hide Actions Pane.

    The actions panes are no longer visible.

Next Steps

You can learn more about how to customize the Office UI from these topics:

See Also

Tasks

How to: Get Started Customizing the Ribbon

How to: Change the Order of Tabs on the Ribbon

How to: Customize a Built-in Tab

How to: Customize the Microsoft Office Menu

Concepts

Accessing the Ribbon at Run Time

Ribbon Designer

Customizing a Ribbon for Outlook

Ribbon Object Model Overview

Other Resources

Ribbon Overview

Change History

Date

History

Reason

September 2010

Added .NET Framework 3.5 code example.

Content bug fix.