Guides & Tutorials (beta)

Adding One or More ToolBars on the Same Row

by Mike Palmatier Visual Basic 6 | C# .NET Created on 10/20/2005  |  Updated on 07/30/2024 0/5 ( 0 votes )

By default, when a toolbar is added, the toolbar appears on a separate row. Each toolbar added will then appear under the previous toolbar. However, many times it is useful to dock a toolbar next to another on the same row using code.

In the picture below, you can see that toolbar #2 has been docked next to toolbar #1. This was done using code.

Toolbars side by side

The code below provides a helper function that will dock a toolbar to the right of another, regardless of the order that the toolbars are added. This code will allow you to dock a toolbar next to another whether it is vertical or horizontal.

'Parameters:
'   BarToDock - ToolBar to be placed to 
'             - the RIGHT of another ToolBar
'   BarOnLeft - ToolBar that will be on the LEFT side of BarToDock
'   VerticalBar - Set to false if the toolbars are docked at the 
'                 top or bottom of the window (horizontal), set to 
'                 true if the toolbars are on the left or right of 
'                 the window (vertical).

'sample usage:
'   Dim ToolBar1 As CommandBar, ToolBar2 As CommandBar
'   ..........Add controls to toolbars
'   DockRightOf ToolBar2, ToolBar1, False
Private Sub DockRightOf(BarToDock As CommandBar, _
            BarOnLeft As CommandBar, VerticalBar As Boolean)
    Dim Left As Long
    Dim Top As Long
    Dim Right As Long
    Dim Bottom As Long
    Dim LeftBar As CommandBar

    Set LeftBar = BarOnLeft

    CommandBars.RecalcLayout
    BarOnLeft.GetWindowRect Left, Top, Right, Bottom

    LeftBar.GetWindowRect Left, Top, Right, Bottom

    If (VerticalBar = False) Then
        CommandBars.DockToolBar BarToDock, Right, _
                         (Bottom + Top) / 2, LeftBar.Position
    Else
        CommandBars.DockToolBar BarToDock, (Left + Right) _
                                / 2, Bottom, LeftBar.Position
    End If
End Sub
/*
Parameters:
   BarToDock - ToolBar to be placed to the RIGHT 
               of another ToolBar
   BarOnLeft - ToolBar that will be on the LEFT side of BarToDock
   VerticalBar - Set to false if the toolbars are docked at the 
                 top or bottom of the window (horizontal), set to 
                 true if the toolbars are on the left or right of 
                 the window (vertical)

Sample usage:
   XtremeCommandBars.CommandBar ToolBar1, ToolBar2;
   //..........Add controls to toolbars
   DockRightOf(ToolBar2, ToolBar1, false);
*/
private void DockRightOf(XtremeCommandBars.CommandBar BarToDock,
    XtremeCommandBars.CommandBar BarOnLeft, Boolean VerticalBar)
{
    int left, top, right, bottom;
    XtremeCommandBars.CommandBar LeftBar = BarOnLeft;

    CommandBars.RecalcLayout();
    LeftBar.GetWindowRect(out left, out top, out right, out bottom);

    if (VerticalBar == false)
        CommandBars.DockToolBar(BarToDock, right,
        (bottom + top) / 2, LeftBar.Position);
    else
        CommandBars.DockToolBar(BarToDock, (left + right) / 2,
        Bottom, LeftBar.Position);
}

User Comments

No comments yet, sign in to comment.

Secure Transactions

Online transactions are secure and protected.


Ssl seal 1
This site is protected by Trustwave's Trusted Commerce program
CompliAssure Secured

Money Back Guarantee

If you are not 100% happy with your purchase within 30 days, you can return it for any reason. Your satisfaction is our priority!


Satisfication Guarantee

Contact sales@codejock.com for more details.