Howto: Manipulate the MiniToolbar

This chapter explains the typical process of manipulating the MiniToolbar.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for Windows Forms\CSharp\Howto\MiniToolbar
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for Windows Forms\VB.NET\Howto\MiniToolbar

Used TX Text Control controls:

  • TXTextControl.TextControl

Relevant API links:

This sample shows how to manipulate the MiniToolbar in order to add contextual content groups.

Inspired by Microsoft Word, the new MiniToolbar is a small version of the full ribbon toolbar providing the most common formatting features. The MiniToolbar appears when text is selected or selected text is right-clicked.

Image

This context sensitive toolbar is very handy for quick formatting tasks without moving the mouse back and forth from the selection to the full ribbon bar.

When right-clicking a selection or an object such as a table or text frame, contextual features are displayed in the MiniToolbar.

The TextControl.ShowMiniToolbar property can be used to control under which circumstances the toolbar should be shown. By default, the MiniToolbar is not shown and must be enabled programmatically to keep applications backwards compatible. In the TextControl.MiniToolbarOpening event, the MiniToolbar can be customized or replaced.

The sample adds a new group to the MiniToolbar to edit hyperlinks when the MiniToolbar is opened by clicking a hyperlink in a document. The following code creates a new RibbonGroup:

private TXTextControl.Windows.Forms.Ribbon.RibbonGroup CreateEditHyperlinkGroup() {
        // Create a ribbon group that contains...
        TXTextControl.Windows.Forms.Ribbon.RibbonGroup rgEditHyperlinkGroup = new TXTextControl.Windows.Forms.Ribbon.RibbonGroup() {
                ShowSeperator = false
        };
        // ... a button to open the TextControl Edit HyperlinkDialog.
        TXTextControl.Windows.Forms.Ribbon.RibbonButton rbtnEditHyperlink = new TXTextControl.Windows.Forms.Ribbon.RibbonButton();
        rbtnEditHyperlink.Text = "Edit Hyperlink";
        rbtnEditHyperlink.LargeIcon = TXTextControl.Windows.Forms.ResourceProvider.GetLargeIcon(TXTextControl.Windows.Forms.Ribbon.RibbonInsertTab.RibbonItem.TXITEM_InsertHyperlink.ToString(), this);
        rbtnEditHyperlink.Click += EditHyperlink_Click;

        // Add the edit hyperlink button to group.
        rgEditHyperlinkGroup.RibbonItems.Add(rbtnEditHyperlink);

        return rgEditHyperlinkGroup;
}
Private Function CreateEditHyperlinkGroup() As TXTextControl.Windows.Forms.Ribbon.RibbonGroup
        ' Create a ribbon group that contains...
        Dim rgEditHyperlinkGroup As TXTextControl.Windows.Forms.Ribbon.RibbonGroup = New TXTextControl.Windows.Forms.Ribbon.RibbonGroup() With {
                .ShowSeperator = False
        }
        ' ... a button to open the TextControl Edit HyperlinkDialog.
        Dim rbtnEditHyperlink As TXTextControl.Windows.Forms.Ribbon.RibbonButton = New TXTextControl.Windows.Forms.Ribbon.RibbonButton()
        rbtnEditHyperlink.Text = "Edit Hyperlink"
        rbtnEditHyperlink.LargeIcon = TXTextControl.Windows.Forms.ResourceProvider.GetLargeIcon(TXTextControl.Windows.Forms.Ribbon.RibbonInsertTab.RibbonItem.TXITEM_InsertHyperlink.ToString(), Me)
        AddHandler rbtnEditHyperlink.Click, AddressOf EditHyperlink_Click

        ' Add the edit hyperlink button to group.
        rgEditHyperlinkGroup.RibbonItems.Add(rbtnEditHyperlink)

        Return rgEditHyperlinkGroup
End Function

This group is added in the TextControl.TextMiniToolbarInitialized event:

private void TextControl_TextMiniToolbarInitialized(object sender, TXTextControl.MiniToolbarInitializedEventArgs e) {
        // Ensure that the TextMiniToolbar's table layout group won't be displayed if the input position is inside a table.
        e.MiniToolbar.RibbonGroups.Remove((e.MiniToolbar as TXTextControl.TextMiniToolbar).FindItem(TXTextControl.TextMiniToolbar.RibbonItem.TXITEM_TableLayoutGroup) as TXTextControl.Windows.Forms.Ribbon.RibbonGroup);

        // Create and add a ribbon group to the TextMiniToolbar that provides an "Edit Hyperlink" button.
        e.MiniToolbar.RibbonGroups.Add(CreateEditHyperlinkGroup());
}
Private Sub TextControl_TextMiniToolbarInitialized(ByVal sender As Object, ByVal e As TXTextControl.MiniToolbarInitializedEventArgs) Handles m_txTextControl.TextMiniToolbarInitialized
        ' Ensure that the TextMiniToolbar's table layout group won't be displayed if the input position is inside a table.
        e.MiniToolbar.RibbonGroups.Remove(TryCast(TryCast(e.MiniToolbar, TXTextControl.TextMiniToolbar).FindItem(TXTextControl.TextMiniToolbar.RibbonItem.TXITEM_TableLayoutGroup), TXTextControl.Windows.Forms.Ribbon.RibbonGroup))

        ' Create and add a ribbon group to the TextMiniToolbar that provides an "Edit Hyperlink" button.
        e.MiniToolbar.RibbonGroups.Add(CreateEditHyperlinkGroup())
End Sub

In the TextControl.MiniToolbarOpening event, contextual groups are hidden dynamically:

private void TextControl_TextMiniToolbarInitialized(object sender, TXTextControl.MiniToolbarInitializedEventArgs e) {
        // Ensure that the TextMiniToolbar's table layout group won't be displayed if the input position is inside a table.
        e.MiniToolbar.RibbonGroups.Remove((e.MiniToolbar as TXTextControl.TextMiniToolbar).FindItem(TXTextControl.TextMiniToolbar.RibbonItem.TXITEM_TableLayoutGroup) as TXTextControl.Windows.Forms.Ribbon.RibbonGroup);

        // Create and add a ribbon group to the TextMiniToolbar that provides an "Edit Hyperlink" button.
        e.MiniToolbar.RibbonGroups.Add(CreateEditHyperlinkGroup());
}
Private Sub TextControl_MiniToolbarOpening(ByVal sender As Object, ByVal e As TXTextControl.MiniToolbarOpeningEventArgs) Handles m_txTextControl.MiniToolbarOpening
        ' Check whether the opening mini tool bar is of type TextMiniToolbar.
        If TypeOf e.MiniToolbar Is TXTextControl.TextMiniToolbar Then
                e.MiniToolbar.RibbonGroups(1).Visible = True   ' Ensure that the TextMiniToolbar's Styles group is always shown (even if the input position is inside a table).

                ' Ensure that the Styles group's separator and the "Edit Hyperlink" group are displayed if
                ' the current context is TextField and the text field is of type TXTextControl.HypertextLink..
                e.MiniToolbar.RibbonGroups(1).ShowSeperator = CSharpImpl.Assign(e.MiniToolbar.RibbonGroups(2).Visible, (e.MiniToolbarContext And TXTextControl.ContextMenuLocation.TextField) = TXTextControl.ContextMenuLocation.TextField AndAlso m_txTextControl.HypertextLinks.GetItem() IsNot Nothing)
        End If
End Sub

When right-clicking a hyperlink in the sample, the MiniToolbar is shown with the new contextual ribbon group:

Image