Back to Devexpress

Bar and Ribbon Merging

windowsforms-16234-controls-and-libraries-application-ui-manager-bar-and-ribbon-merging-bar-and-ribbon-merging.md

latest14.6 KB
Original Source

Bar and Ribbon Merging

  • Sep 16, 2022
  • 9 minutes to read

The Document Manager component supports the merging of RibbonControl and BarManager controls. When this feature is enabled, Bar Item Links contained within Documents are merged to corresponding Bars and Ribbon Page Groups within the parent form. You can find more info about Bar and Ribbon merging mechanics in the MDI Merging and Ribbon Merging topics.

In this topic, you will learn how to merge BarManager components within a Document Manager.

  1. Start Visual Studio and create a new Windows Forms Application project. Enter the project’s name, choose the solution folder, and click OK.

  2. Find the Document Manager component in the toolbox and drop it onto the form. By default, the Document Manager applies the Tabbed View to display its Documents. You can change the current Document Manager View by clicking the Convert to… links within the Tasks menu, which is invoked by clicking the component’s smart tag (see the figure below). For this example, you will use the Tabbed View , so leave the Convert to… settings unchanged and invoke the Document Manager designer by clicking the Run Designer link.

  3. Switch to the Documents section in the Main tab. Click Add New Document to create the required number of tabbed Documents.

  4. The Documents created in the previous step are empty. If you try to launch the app now, you will get the ‘Deferred Loading Exception’, which asks you to set the Document content. In this example, you will use a UserControl for the content. Right-click the project in the Visual Studio Solution Explorer window and select Add | New Item.

  5. Drop the BarManager and RichEditControl components onto the User Control. Once dropped, the Bar Manager creates three child Bar objects - Main Menu, Status Bar and a regular Toolbar. We will not need the Main Menu bar, so select it and press the Delete key to remove it. To add items to the Toolbar and Status Bar, click the [Add] label within the required Bar and select the desired item type. Next, add two BarButtonItem objects to the Toolbar (Undo and Redo) and a BarStaticItem object to the Status Bar. The figure below displays the approximate result.

  6. Now add the Bar Manager for the main application form and customize its bars. Refer to the previous step if you have any difficulties. The figure below illustrates the main application form with a customized Bar Manager.

  7. Finally, merge Bar Manager from the UserControl with the main form’s BarManager. First, set the DocumentManager.RibbonAndBarsMergeStyle property to Always.

Note

In this sample, you can also see another Document Manager feature - Microsoft Visual Studio-like behavior. When you click BarItemLinks within the main form’s RibbonControl, child Documents do not loose their focus. In this example, the feature allows the ‘Undo’ and ‘Redo’ buttons to perform required operations exactly on the currently focused RichEditControl.

Complete Code

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocumentManagerBarMerging {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocumentManagerBarMerging {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
            tabbedView1.DocumentProperties.AllowPin = true;
            documentManager1.RibbonAndBarsMergeStyle = DevExpress.XtraBars.Docking2010.Views.RibbonAndBarsMergeStyle.Always;
        }

        private void tabbedView1_QueryControl(object sender, DevExpress.XtraBars.Docking2010.Views.QueryControlEventArgs e) {
            e.Control = new ucContent();
        }

        private void Dock(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (tabbedView1.ActiveDocument != null) tabbedView1.Controller.Float(tabbedView1.ActiveDocument);
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (tabbedView1.ActiveFloatDocument != null) tabbedView1.Controller.Dock(tabbedView1.ActiveFloatDocument);
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (tabbedView1.ActiveDocument != null) (tabbedView1.ActiveDocument as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document).Pinned = true;
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (tabbedView1.ActiveDocument != null) (tabbedView1.ActiveDocument as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document).Pinned = false;
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            tabbedView1.Controller.ResetLayout();
        }

        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            tabbedView1.Controller.SelectNextTab(true);
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            tabbedView1.Controller.SelectNextTab(false);
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            DevExpress.XtraBars.Docking2010.Views.Tabbed.Document newDoc = tabbedView1.AddDocument(new ucContent()) as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document;
            newDoc.Caption = "New Document " + (tabbedView1.Documents.Count).ToString();
        }

        private void barManager1_Merge(object sender, DevExpress.XtraBars.BarManagerMergeEventArgs e) {
            bar1.Merge(e.ChildManager.Bars[0]);
            barManager1.StatusBar.Merge(e.ChildManager.StatusBar);
        }

        private void barManager1_UnMerge(object sender, DevExpress.XtraBars.BarManagerMergeEventArgs e) {
            bar1.UnMerge();
            barManager1.StatusBar.UnMerge();
        }
    }
}
csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocumentManagerBarMerging {
    public partial class ucContent : UserControl {
        public ucContent() {
            InitializeComponent();
            barButtonItem1.Enabled = barButtonItem2.Enabled = false;
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            richEditControl1.Undo();
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            richEditControl1.Redo();
        }

        private void richEditControl1_TextChanged(object sender, EventArgs e) {
            if (true == richEditControl1.CanUndo) barButtonItem1.Enabled = true;
                else barButtonItem1.Enabled = false;
            if (true == richEditControl1.CanRedo) barButtonItem2.Enabled = true;
                else barButtonItem2.Enabled = false;
        }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace DocumentManagerBarMerging
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace DocumentManagerBarMerging
    Partial Public Class ucContent
        Inherits UserControl

        Public Sub New()
            InitializeComponent()
            barButtonItem2.Enabled = False
            barButtonItem1.Enabled = barButtonItem2.Enabled
        End Sub

        Private Sub barButtonItem1_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem1.ItemClick
            richEditControl1.Undo()
        End Sub

        Private Sub barButtonItem2_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem2.ItemClick
            richEditControl1.Redo()
        End Sub

        Private Sub richEditControl1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles richEditControl1.TextChanged
            If True = richEditControl1.CanUndo Then
                barButtonItem1.Enabled = True
                Else
                    barButtonItem1.Enabled = False
                End If
            If True = richEditControl1.CanRedo Then
                barButtonItem2.Enabled = True
                Else
                    barButtonItem2.Enabled = False
                End If
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace DocumentManagerBarMerging
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
            tabbedView1.DocumentProperties.AllowPin = True
            documentManager1.RibbonAndBarsMergeStyle = DevExpress.XtraBars.Docking2010.Views.RibbonAndBarsMergeStyle.Always
        End Sub

        Private Sub tabbedView1_QueryControl(ByVal sender As Object, ByVal e As DevExpress.XtraBars.Docking2010.Views.QueryControlEventArgs) Handles tabbedView1.QueryControl
            e.Control = New ucContent()
        End Sub

        Private Overloads Sub Dock(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem1.ItemClick
            If tabbedView1.ActiveDocument IsNot Nothing Then
                tabbedView1.Controller.Float(tabbedView1.ActiveDocument)
            End If
        End Sub

        Private Sub barButtonItem2_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem2.ItemClick
            If tabbedView1.ActiveFloatDocument IsNot Nothing Then
                tabbedView1.Controller.Dock(tabbedView1.ActiveFloatDocument)
            End If
        End Sub

        Private Sub barButtonItem3_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem3.ItemClick
            If tabbedView1.ActiveDocument IsNot Nothing Then
                TryCast(tabbedView1.ActiveDocument, DevExpress.XtraBars.Docking2010.Views.Tabbed.Document).Pinned = True
            End If
        End Sub

        Private Sub barButtonItem4_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem4.ItemClick
            If tabbedView1.ActiveDocument IsNot Nothing Then
                TryCast(tabbedView1.ActiveDocument, DevExpress.XtraBars.Docking2010.Views.Tabbed.Document).Pinned = False
            End If
        End Sub

        Private Sub barButtonItem5_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem5.ItemClick
            tabbedView1.Controller.ResetLayout()
        End Sub

        Private Sub barButtonItem6_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem6.ItemClick
            tabbedView1.Controller.SelectNextTab(True)
        End Sub

        Private Sub barButtonItem7_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem7.ItemClick
            tabbedView1.Controller.SelectNextTab(False)
        End Sub

        Private Sub barButtonItem8_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barButtonItem8.ItemClick
            Dim newDoc As DevExpress.XtraBars.Docking2010.Views.Tabbed.Document = TryCast(tabbedView1.AddDocument(New ucContent()), DevExpress.XtraBars.Docking2010.Views.Tabbed.Document)
            newDoc.Caption = "New Document " & (tabbedView1.Documents.Count).ToString()
        End Sub

        Private Sub barManager1_Merge(ByVal sender As Object, ByVal e As DevExpress.XtraBars.BarManagerMergeEventArgs) Handles barManager1.Merge
            bar1.Merge(e.ChildManager.Bars(0))
            barManager1.StatusBar.Merge(e.ChildManager.StatusBar)
        End Sub

        Private Sub barManager1_UnMerge(ByVal sender As Object, ByVal e As DevExpress.XtraBars.BarManagerMergeEventArgs) Handles barManager1.UnMerge
            bar1.UnMerge()
            barManager1.StatusBar.UnMerge()
        End Sub
    End Class
End Namespace