ComponentOne Ribbon for WinForms
C1Ribbon (Classic) Task-Based Help / Adding Ribbon Items / Adding a Contextual Tab to the Ribbon
In This Topic
    Adding a Contextual Tab to the Ribbon
    In This Topic

    The actions that users need to use regularly should always be available on the Ribbon control, where the user can access them within a few clicks. However, certain actions are exclusive to a certain element; for example, an action that allows you to format the font of text may only be necessary when users are editing text in a RichTextBox. In the aforementioned case, it would be beneficial to place this action (and all other actions specific to RichTextBoxes) under a contextual tabs that only appears when users have selected the RichTextBox. This topic demonstrates how to add a contextual tab group that appears only when a RichTextBox is selected.

    Complete the following steps:

    1. From the Visual Studio Toolbox, add the following controls to your Windows form:
      • (1) C1Ribbon control
      • (1) ListBox control
      • (1) RichTextBox control
    2. Arrange the form as follows:
      C1Ribbon form
    3. Hover over the Ribbon to enable the floating toolbar, click the Actions button actionsbutton, and select Add Contextual Tab Group.
    4. Select View | Code to enter Code view and add the following code to your project:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub richTextBox1_Enter(sender As Object, e As EventArgs)
         ribbonContextualTabGroup1.Visible = True
      End Sub
      Private Sub richTextBox1_Leave(sender As Object, e As EventArgs)
         ribbonContextualTabGroup1.Visible = False
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void richTextBox1_Enter(object sender, EventArgs e)
      {
                 ribbonContextualTabGroup1.Visible = true;
      }
      private void richTextBox1_Leave(object sender, EventArgs e)
      {
                  ribbonContextualTabGroup1.Visible = false;
      }
      
    5. Select View | Designer to return to Design view.
    6. Complete the following actions in the Properties window:
      • From the drop-down list, select c1Ribbon1 and then set the Selectable property to False.
      • From the drop-down list, select ribbonContextualTabGroup1 and then set the Visible property to False.
      • From the drop-down list, select richTextBox1, click the Events button eventsbutton, and then set the following event handlers so that the RichTextBox control can handle the code you added to the project:
        • Set Enter to richTextBox1_Enter.
        • Set Leave to richTextBox1_Leave.
    7. Press F5 to run the project. Observe that the contextual tab that you added to the project is hidden.
      contextual tab added
    8. Click inside the RichTextBox and observe that the contextual tab appears in orange.
      click within rich text box
    9. Click inside the ListBox and observe that the contextual tab disappears.
      click within listbox

    And that’s it! Ribbon for WinForms makes it that simple to add a contextual tab to the Ribbon interface.