Ribbon for WinForms
コンテキストメニュー
設計時のサポート > コンテキストメニュー

Ribbon provides additional commands for each ribbon item through context menu. To access Ribbon item's context menu, right-click on the ribbon item and the context menu appears as following:

RibbonItem context menu

The following table demonstrates the commands provided by RibbonItem context menu:

Commands Descriptions
Add to Quick Access Toolbar Adds the item to Quick Access Toolbar.
Minimize the Ribbon Minimizes the Ribbon

Customize Context Menu

Ribbon provides access to some basic operations through the context menu. However, there might be a scenario where you would want to access custom operations via context menu. For instance, you want to facilitate end-users to hide ribbon items as per their requirement using the context menu as shown in the following image.

custom context menu

To display a customized context menu for the ribbon item, follow these steps:

  1. Add the custom menu item to the collection of menu items using the Add method. In this example, we add a RibbonButton item to the context menu.
    C#
    コードのコピー
    RibbonButton button1 = new RibbonButton();
    button1.Text = "Hide";
    //リボンの ContextMenu にカスタム項目を追加します。
    c1Ribbon1.ContextMenuItems.Add(button1);
    

  2. Subscribe to the MouseUp event to display a customized context menu for a radio button using the following code.
    C#
    コードのコピー
    c1Ribbon1.MouseUp += C1Ribbon1_MouseUp;
    

  3. Add the following code to the C1Ribbon1_MouseUp event handler to use custom context menu instead of the default context menu. In this example, we display a custom context menu for a radio button in Ribbon using ShowContextMenu method of the C1Ribbon class. This method displays the contextual menu at the specified screen position.
    C#
    コードのコピー
    private void C1Ribbon1_MouseUp(object? sender, MouseEventArgs e)
       {
            if (e.Button != MouseButtons.Right) return;
            var rItem = c1Ribbon1.GetItemAt(e.Location);
                if (rItem != null && rItem is RibbonItem ri)
                {
                    c1Ribbon1.ShowContextMenu(this.PointToScreen(e.Location), ri);
                }
       }
    

    By default, the ShowContextMenu method displays customized context menu for ribbon items only and not for child ribbon items. However, if you want to display a customized context menu for child ribbon items as well, you can use UseCustomMenu property of the ContextMenuPopUpEventArgs class as demonstrated in the following steps.
    1. Subscribe to ContextMenuPopUp event of the C1Ribbon class as shown in the following code.
      C#
      コードのコピー
      c1Ribbon1.ContextMenuPopUp += C1Ribbon1_ContextMenuPopUp;
      

    2. Add the following code to the C1Ribbon1_ContextMenuPopup event handler to show custom context menu instead of the default menu using the UseCustomMenu property.
      C#
      コードのコピー
      private void C1Ribbon1_ContextMenuPopup(object? sender, C1.Win.Ribbon.ContextMenuPopupEventArgs e)
         {           
             //ContextMenu が ribbonButton2 に対して開いている場合。
             if(e.Component is RibbonButton ribbonButton2 && ribbonButton2.Name == "GenderBtn")
             {
                  //上記で追加されたデフォルト メニューの代わりに、カスタム メニューを使用します。
                  e.UseCustomMenu = true;
             }
         }
      

Alternatively, you can use RibbonMenuItems Collection Editor to add custom items to the context menu. To access RibbonMenuItems Collection Editor, click the ellipsis next to the ContextMenuItems property from the Properties Window. The RibbonMenuItems Collection Editor appears as following:

RibbonMenuItems Collection Editor

In RibbonMenuItems Collection Editor, the right pane displays properties for each defined ribbon menu item. The left pane displays a list of menu items along with Add and Remove buttons which can be used to add and remove menu items, respectively.