A Simple C# Toolbar Docking Framework






4.88/5 (92 votes)
Sep 1, 2004
3 min read

482419

6832
An example framework for toolbar handling, written in C#.
- Download source files (version 1) - 11 Kb
- Download source files (version 2) - 12 Kb
- Download demo project (version 2) - 39 Kb
Introduction
This code snippet includes a simple framework for handling docking toolbars. This framework includes support for:
- floating/docking bars
- changing layout of docked bars
- double-click to switch from docked to floating and back
- show/hide bar
- right-click for bar view menu
- disabling docking using the control key
- handles any kind of control, not just toolbars
The code runs on Windows XP and VS .NET 2002, but should work on most .NET environments.
Background
The code was written while exploring the .NET framework. Although several toolbar handling implementations are available, I decided to have a go at writing my own. Nevertheless, I hope this article might be of use.
Using the code
Using the code is quite simple. Create a ToolBarManager
object,
giving it the form where the toolbars may be docked. This form would most
likely be your application main form which would also keep the ToolBarManager
as a member.
_toolBarManager = new ToolBarManager(this);
The ToolBarManager
handles all UI operations on the toolbars and it
is also the only class the programmer will access. Adding toolbars is performed
by invoking the AddControl
method. Different versions of this
method are available to allow more control over the positioning of the new
toolbar.
// Add toolbar (default position)
_toolBarManager.AddControl(_toolBar1);
// Add toolbar (floating)
_toolBarManager.AddControl(_toolBar2, DockStyle.None);
// Add toolbar (left)
_toolBarManager.AddControl(_toolBar3, DockStyle.Left);
// Add toolbar (left, on the left of _toolBar3)
_toolBarManager.AddControl(_toolBar4, DockStyle.Left, _toolBar3, DockStyle.Left);
// Add control
_toolBarManager.AddControl(_dateTimePicker, DockStyle.Bottom);
Other methods are also available to access the toolbars being handled by the ToolBarManager
.
Their use is rather straightforward:
-
public ArrayList GetControls()
- Returns the set of all the added controls. -
public bool ContainsControl(Control c)
- Returnstrue
if the control is included. -
public void ShowControl(Control c, bool show)
- Shows or hides the control. -
public void RemoveControl(Control c)
- Removes the control.
Points of Interest
The docking behavior is performed by four ToolBarDockArea
controls
that are added and docked to all four sides of the form. These controls simply
perform specific layout handling and automatic resizing according to the
controls that are added or changed.
To achieve dragging, the ToolBarManager
handles mouse events on all
controls added via AddControl
. During drags, the ToolBarManager
checks, according to the mouse position, if the dragged control should be
placed on its own form (floating) or on any of the dock areas.
The control key events are handled via PreFilterMessage
.
The appearance of the toolbar (the floating frame and the gripper) are user
drawn in the ToolBarDockHolder
user control.
History
A history file with detailed information is included in the download, here is a summary.
Version 1.0:
First version by Rogério Paulo.
Version 2.0:
Updates by Martin Müller (aka mav.northwind):
-
ToolBarManager
c'tor now takes aScrollableControl
and a Form so that you can define an independent docking region and do not always have to use the form. The form itself is still needed as owner of floating toolbars, though. -
ToolBarDockHolder
now has a new propertyToolbarTitle
, allowing you to specify an independent name for a floating toolbar. The default is still the control's Text, but theControl.TextChanged
event will no longer modify the title. -
ToolBarDockHolder
now has a new propertyAllowedBorders
where you can specify which borders the toolbar is allowed to dock to. -
ToolBarManager.AddControl()
now returns the newly addedToolBarDockHolder
, so that it's properties can be modified. The example now includes aMainMenu
- Modified the size calculation for vertical toolbars with separators