Udostępnij za pośrednictwem


RibbonButton Class (2007 System)

Represents a button on a custom Ribbon.

Namespace:  Microsoft.Office.Tools.Ribbon
Assembly:  Microsoft.Office.Tools.Common.v9.0 (in Microsoft.Office.Tools.Common.v9.0.dll)

Syntax

'Declaration
<ToolboxBitmapAttribute(GetType(RibbonButton), "RibbonButton.bmp")> _
Public NotInheritable Class RibbonButton _
    Inherits RibbonControl
'Usage
Dim instance As RibbonButton
[ToolboxBitmapAttribute(typeof(RibbonButton), "RibbonButton.bmp")]
public sealed class RibbonButton : RibbonControl
[ToolboxBitmapAttribute(typeof(RibbonButton), L"RibbonButton.bmp")]
public ref class RibbonButton sealed : public RibbonControl
public final class RibbonButton extends RibbonControl

Remarks

Visual Studio Tools for Office creates an instance of the RibbonButton class when you drag a Button control from the Office Ribbon Controls tab of the Toolbox onto the Ribbon Designer.

You can also create an instance of this control at run time and add it to a dynamic menu. A menu is dynamic if you set its Dynamic property to true.

Some properties of this control can be set only before the Ribbon is loaded into the Office application or before the control is added to a dynamic menu at run time. For information about setting these properties, see Ribbon Object Model Overview.

Common Tasks

The following table lists members that are useful for common tasks.

Task

Member

At run time, add a button to a menu that has its Dynamic property set to true.

Create an instance of the RibbonButton class by using the RibbonButton constructor.

Add the instance to the menu's Items collection.

Change the size of the button.

Set ControlSize to the value you want. For example, for a large button, set it to Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge.

Display an image on the button.

There are three ways to display an image:

  • Use the Image property. This property gives the most control.

  • Set OfficeImageId to the ID of a built-in Microsoft Office image that you want to display as the icon for the button.

  • Use the ImageName property along with the LoadImage event. This property gives some performance benefits.

Disable or hide buttons.

To disable a button, set Enabled to false.

To hide a button, set Visible to false.

Run code when a user clicks the button.

Add code to the Click event handler.

Examples

The following example creates a new button, sets various properties of the button, and then adds the button to a menu. To run this code example, you must first perform the following steps:

  1. Add a Ribbon (Visual Designer) item to a Visual Studio Tools for Office project.

  2. Add a group to the custom tab.

  3. Add a menu to the group.

  4. Set the Dynamic property of the menu to true.

  5. Set the ItemSize property of the menu to RibbonControlSizeLarge.

Private Sub AddButtonsToMenu()
    Dim tempButton As New RibbonButton()
    tempButton.Label = "Button 1"
    tempButton.ControlSize = _
        Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge
    tempButton.Description = "My Ribbon Button"
    tempButton.ShowImage = True
    tempButton.ShowImage = True
    tempButton.Image = My.Resources.Image1
    tempButton.Image = My.Resources.Image2
    tempButton.KeyTip = "A1"
    menu1.Items.Add(tempButton)
End Sub
private void AddButtonsToMenu()
{
    RibbonButton tempButton = new RibbonButton();
    tempButton.Label = "Button 1";
    tempButton.ControlSize = 
        Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
    tempButton.Description = "My Ribbon Button";
    tempButton.ShowImage = true;
    tempButton.ShowImage = true;
    tempButton.Image = Properties.Resources.Image1;
    tempButton.Image = Properties.Resources.Image2;
    tempButton.KeyTip = "A1";
    menu1.Items.Add(tempButton);

}

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      Microsoft.Office.Tools.Ribbon.RibbonComponent
        Microsoft.Office.Tools.Ribbon.RibbonControl
          Microsoft.Office.Tools.Ribbon.RibbonButton

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

RibbonButton Members

Microsoft.Office.Tools.Ribbon Namespace

RibbonMenu

RibbonButton

Other Resources

Ribbon Overview

Ribbon Designer

Ribbon Object Model Overview

How to: Get Started Customizing the Ribbon