Modifying the Ribbon bar

TX Text Control .NET for Windows Forms is shipped with a fully-featured, customizable and programmable Ribbon Bar with pre-configured Ribbon Tabs for the most typical applications of TX Text Control. When clicking the buttons in the sample project, buttons are removed, ribbon groups are removed or complete tabs are created with a group and a button.

Changing the ribbon item's text

The first button Change RibbonItem Text (Paste button) uses the Windows.Forms.Ribbon.RibbonFormattingTab.FindItem method to retrieve the Paste Windows.Forms.Ribbon.RibbonSplitButton. Using the Windows.Forms.Ribbon.RibbonButton.Text property, the text of the button can be changed:

[C#]
RibbonSplitButton ribbonItem = (RibbonSplitButton)ribbonFormattingTab1.FindItem(
        RibbonFormattingTab.RibbonItem.TXITEM_Paste);

ribbonItem.Text = "New Text";
[Visual Basic]
Dim ribbonItem As RibbonSplitButton = DirectCast(
        ribbonFormattingTab1.FindItem(RibbonFormattingTab.RibbonItem.TXITEM_Paste), RibbonSplitButton)

ribbonItem.Text = "New Text"

Adding events to ribbon items

The second button adds the Windows.Forms.Ribbon.RibbonSplitButton.ButtonClick event to the split button:

[C#]
RibbonSplitButton ribbonItem = (RibbonSplitButton)ribbonFormattingTab1.FindItem(
        RibbonFormattingTab.RibbonItem.TXITEM_Paste);

ribbonItem.ButtonClick += RibbonItem_ButtonClick;
[Visual Basic]
Dim ribbonItem As RibbonSplitButton = DirectCast(
        ribbonFormattingTab1.FindItem(RibbonFormattingTab.RibbonItem.TXITEM_Paste), RibbonSplitButton)

AddHandler ribbonItem.ButtonClick, AddressOf RibbonItem_ButtonClick

Removing ribbon items

This button removes a ribbon item from it's parent container, the Windows.Forms.Ribbon.RibbonGroup:

[C#]
RibbonSplitButton ribbonItem = (RibbonSplitButton)ribbonFormattingTab1.FindItem(
        RibbonFormattingTab.RibbonItem.TXITEM_Paste);

if(ribbonItem.ParentCollection != null)
        ribbonItem.ParentCollection.Remove(ribbonItem);
[Visual Basic]
Dim ribbonItem As RibbonSplitButton = DirectCast(
        ribbonFormattingTab1.FindItem(RibbonFormattingTab.RibbonItem.TXITEM_Paste), RibbonSplitButton)

If ribbonItem.ParentCollection IsNot Nothing Then
        ribbonItem.ParentCollection.Remove(ribbonItem)
End If

Removing ribbon groups

When clicking the button Remove RibbonGroup (Font), the complete Font ribbon group gets removed:

[C#]
foreach (RibbonGroup group in ribbonFormattingTab1.RibbonGroups)
{
    if (group.Name == "TXITEM_FontGroup")
    {
        ribbonFormattingTab1.RibbonGroups.Remove(group);
        break;
    }
}
[Visual Basic]
For Each group As RibbonGroup In ribbonFormattingTab1.RibbonGroups
    If group.Name = "TXITEM_FontGroup" Then
        ribbonFormattingTab1.RibbonGroups.Remove(group)
        Exit For
    End If
Next

Removing ribbon tabs

Complete ribbon tabs can be removed using the Controls property of the Windows.Forms.Ribbon.Ribbon control:

[C#]
ribbon1.Controls.Remove(ribbonInsertTab1);
[Visual Basic]
ribbon1.Controls.Remove(ribbonInsertTab1)

Creating new ribbon tabs

The event handler of the last button creates a complete new Windows.Forms.Ribbon.RibbonTab with a new Windows.Forms.Ribbon.RibbonGroup. Additionally, a new Windows.Forms.Ribbon.RibbonButton is created and added to the created ribbon group. Finally, the ribbon tab is added to the Controls collection of the ribbon control and the selected tab is specified using the SelectedIndex property.

[C#]
// create a new RibbonTab control and set the text (tab title)
RibbonTab myRibbonTab = new RibbonTab();
myRibbonTab.Text = "MyRibbonTab";

// create a new RibbonGroup
RibbonGroup myRibbonGroup = new RibbonGroup() { Text = "My Group" };

// make the dialog launcher icon invisible
myRibbonGroup.DialogBoxLauncher.Visible = false;

// add the new RibbonGroup to the RibbonGroup collection
// of the newly created RibbonTab
myRibbonTab.RibbonGroups.Add(myRibbonGroup);

// create a new RibbonButton
RibbonButton rbtnClickMe = new RibbonButton()
{
    Text = "Click me!",
    IsAddToQuickAccessToolbarEnabled = true,
};

// set some properties of the button and attach a "Click" event
rbtnClickMe.ToolTip.Title = "Tooltip Title";
rbtnClickMe.ToolTip.Description = "Tooltip Description";
rbtnClickMe.DisplayMode = IconTextRelation.LargeIconLabeled;
rbtnClickMe.LargeIcon = Image.FromFile("previewclose.png");
rbtnClickMe.Click += RbtnClickMe_Click;
rbtnClickMe.BackColor = Color.LawnGreen;

// add the button to the RibbonGroup
myRibbonGroup.RibbonItems.Add(rbtnClickMe);

// add the newly created RibbonTab to the Ribbon control
ribbon1.Controls.Add(myRibbonTab);

// set the selected tab to the new RibbonTab
ribbon1.SelectedIndex = ribbon1.TabCount - 1;
[Visual Basic]
' create a new RibbonTab control and set the text (tab title)
Dim myRibbonTab As New RibbonTab()
myRibbonTab.Text = "MyRibbonTab"

' create a new RibbonGroup
Dim myRibbonGroup As New RibbonGroup() With { _
    .Text = "My Group" _
}

' make the dialog launcher icon invisible
myRibbonGroup.DialogBoxLauncher.Visible = False

' add the new RibbonGroup to the RibbonGroup collection
' of the newly created RibbonTab
myRibbonTab.RibbonGroups.Add(myRibbonGroup)

' create a new RibbonButton
Dim rbtnClickMe As New RibbonButton() With { _
    .Text = "Click me!", _
    .IsAddToQuickAccessToolbarEnabled = True _
}

' set some properties of the button and attach a "Click" event
rbtnClickMe.ToolTip.Title = "Tooltip Title"
rbtnClickMe.ToolTip.Description = "Tooltip Description"
rbtnClickMe.DisplayMode = IconTextRelation.LargeIconLabeled
rbtnClickMe.LargeIcon = Image.FromFile("previewclose.png")
AddHandler rbtnClickMe.Click, AddressOf RbtnClickMe_Click
rbtnClickMe.BackColor = Color.LawnGreen

' add the button to the RibbonGroup
myRibbonGroup.RibbonItems.Add(rbtnClickMe)

' add the newly created RibbonTab to the Ribbon control
ribbon1.Controls.Add(myRibbonTab)

' set the selected tab to the new RibbonTab
ribbon1.SelectedIndex = ribbon1.TabCount - 1