Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

A Simple C# Toolbar Docking Framework

Rate me:
Please Sign up or sign in to vote.
4.88/5 (95 votes)
7 Sep 20043 min read 471.5K   6.8K   179   108
An example framework for toolbar handling, written in C#.

Sample Image - ToolBarDock.gif

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.

C#
_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.

C#
// 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) - Returns
    C#
    true
    
    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 a ScrollableControl 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 property ToolbarTitle, allowing you to specify an independent name for a floating toolbar. The default is still the control's Text, but the Control.TextChanged event will no longer modify the title.
  • ToolBarDockHolder now has a new property AllowedBorders where you can specify which borders the toolbar is allowed to dock to.
  • ToolBarManager.AddControl() now returns the newly added ToolBarDockHolder, so that it's properties can be modified. The example now includes a MainMenu
  • Modified the size calculation for vertical toolbars with separators

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Portugal Portugal
Rogério Paulo is a software engineer in the domain of industrial information technology since 1999.
He has been mainly involved in product development in the fields of SCADA, engineering toolsets, embedded systems, communication networks and industrial communication protocols.
www.rpaulo.com

Comments and Discussions

 
GeneralRe: Add multiple control to holder Pin
vutle24-Feb-06 9:53
vutle24-Feb-06 9:53 
GeneralSave Load request Pin
Budweizzer23-Feb-06 8:24
Budweizzer23-Feb-06 8:24 
GeneralRe: Save Load request Pin
jonh25-Feb-06 0:22
jonh25-Feb-06 0:22 
GeneralRe: Save Load request Pin
Budweizzer25-Feb-06 10:59
Budweizzer25-Feb-06 10:59 
GeneralRe: Save Load request Pin
Sorin Militaru22-Jun-06 22:20
Sorin Militaru22-Jun-06 22:20 
GeneralRe: Save Load request Pin
Sorin Militaru22-Jun-06 22:45
Sorin Militaru22-Jun-06 22:45 
GeneralRe: Save Load request Pin
jfk95521810-Jul-06 16:21
jfk95521810-Jul-06 16:21 
GeneralRe: Save Load request Pin
chinnilu18-Jul-06 16:12
chinnilu18-Jul-06 16:12 
GeneralRe: Save Load request Pin
Jesua18-Dec-06 5:33
Jesua18-Dec-06 5:33 
GeneralRe: Save Load request Pin
opndesk8-Feb-07 17:49
opndesk8-Feb-07 17:49 
GeneralRe: Save Load request Pin
xu_xuthus5-Mar-07 21:00
xu_xuthus5-Mar-07 21:00 
GeneralRe: Save Load request Pin
jonh6-Mar-07 8:09
jonh6-Mar-07 8:09 
GeneralRe: Save Load request Pin
Miss Litchi19-Mar-07 0:28
Miss Litchi19-Mar-07 0:28 
GeneralRe: Save Load request Pin
jonh19-Mar-07 8:35
jonh19-Mar-07 8:35 
GeneralRe: Save Load request Pin
Miss Litchi29-Mar-07 6:24
Miss Litchi29-Mar-07 6:24 
GeneralRe: Save Load request Pin
jonh29-Mar-07 7:49
jonh29-Mar-07 7:49 
GeneralRe: Save Load request Pin
Venki563-Oct-07 21:37
Venki563-Oct-07 21:37 
GeneralRe: Save Load request Pin
jonh4-Oct-07 10:33
jonh4-Oct-07 10:33 
GeneralRe: Save Load request Pin
lucia11022-Jul-08 3:42
lucia11022-Jul-08 3:42 
QuestionHow about a window (app) that docks to the Desktop??? Pin
GSL33-Feb-06 15:07
GSL33-Feb-06 15:07 
Generalrequest Pin
stalindaa8-Jan-06 11:38
stalindaa8-Jan-06 11:38 
QuestionWonder full .... but if want to lock the tool bar how can i do that? Pin
Rahman Masudur30-Dec-05 22:40
Rahman Masudur30-Dec-05 22:40 
QuestionDragging Problem Pin
Mohammad Hamed26-Dec-05 22:27
Mohammad Hamed26-Dec-05 22:27 
QuestionCan it stay put when scrolling? Pin
Bkins22-Dec-05 13:30
Bkins22-Dec-05 13:30 
AnswerRe: Can it stay put when scrolling? Pin
Rogério Paulo22-Dec-05 23:51
Rogério Paulo22-Dec-05 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.