5,699,431 members and growing! (24,700 online)
Email Password   helpLost your password?
Desktop Development » Menus » Docking bars     Intermediate License: The Code Project Open License (CPOL)

OutlookBar: A Simplified Outlook Style Sidebar Control

By Herre Kuijpers

An article on using and writing a user control resembling one of Outlook's sidebar controls.
C#.NET 2.0, Win2K, WinXP, Win2003, Windows, .NET, WinForms, Visual Studio, VS2005, Dev

Posted: 26 May 2006
Updated: 10 Nov 2006
Views: 129,154
Bookmarked: 179 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
37 votes for this Article.
Popularity: 7.30 Rating: 4.65 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 5.4%
3
12 votes, 32.4%
4
23 votes, 62.2%
5

Sample Image - OutlookbarApp1.png

Introduction

I'm currently working on a personal project in which I want to build a small application with an Outlook 2003 style of navigation. This includes the Outlook sidebar containing the colorful buttons. I have been looking for this control here on CodeProject, so far I could only find one written in C++ with WTL.

I decided to write my own Outlook sidebar usercontrol in and for C# (2.0) so it can be easily extended and custimized to the developer's need. This project therefore contains all the sources which may be modified by any means and for any purpose.

Note that the usercontrol does not contain any 2.0 runtime specific code so it should be fairly easy to port it back to 1.1 projects if needed.

Background

I chose to implement a usercontrol because it simplifies the solution, it is easy to debug, and compiles right into your application's executable leaving a very small footprint (that is no additional DLLs are needed). Reuse of this control is simply a matter of copy and pasting the usercontrol sources to your next project.

Because I wanted to write code that is easy to understand and modify, the OutlookBar control is a simplified version of Outlook's sidebar. Also, the control does not make use of complicated hooks, callbacks, and Windows APIs. After all, we just want to program C#!

Using the code

Simply include the Outlookbar.cs, OutlookBar.Designer.cs, and OutlookBar.resx files into your project. Before using the OutlookBar usercontrol on your forms, make sure you compile everything first. After that, the usercontrol is added to your toolbox. You can now drag it onto your form.  

Note that the OutlookBar control is meant to be docked at the bottom. This is not done automatically so you have to set the Dock property of the control to Bottom. Additionally, you can add a splitter control on top, this will allow you to resize the OutlookBar at runtime.

The OutlookBar contains a set of buttons (of type OutlookBarButton) that can be added to the usercontrol at runtime. In this updated version of the control, it is now possible to setup the buttons at design time! Thanks to Daniel Zaharia's excellent article on persisting control collections simply by making use of the [DesignerSerializationVisibility()] attribute.

However, to add buttons at runtime, you can put the following code in the Form_Load event. Make sure that you add the OutlookStyleControls namespace to the form.

    // image1 to imagen are expected to be initialized and loaded. these images

    // represent the icons on the button (max dimensions 30x26).


    outlookBar1.Buttons.Add("Button 1", image1);
    outlookBar1.Buttons.Add("Button 2", image2);
    ...
    outlookBar1.Buttons.Add("Button n", imagen);
    outlookBar1.Height = outlookBar1.MaximumSize.Height;

Notice the last line. The OutlookBar will resize itself automatically to the dimensions of the buttons. After adding all the buttons, maximize the control to its maximum allowed height. All buttons will then be visible on the form.

At runtime, clicking one of the buttons will generate a Click event. To determine which of the buttons is clicked, you can use the following code:

private void outlookBar1_Click(object sender, 
        OutlookStyleControls.OutlookBar.ButtonClickEventArgs e)
    {
        int idx = outlookBar1.Buttons.IndexOf(e.Button);
        switch (idx)
        {
            case 0: // button 1 was clicked

                // code your action for button1 here

                break;
            case 1: // button 2 was clicked

                // code your action for button1 here

                break;
            ...
        }   
    }
OutlookBar interface design That's all there is to it. The control contains some additional features:
  • Setup your OutlookBar buttons at design time.
  • The HitTest() method of the user control returns the button at a specific location. E.g., it is useful in the MouseDown or MouseUp events.
  • The color schemes used by the control can be set programmatically.
  • Each button on the control has a Tag property you can use to bind additional information to the button.
  • The ButtonHeight property, which allows you to display either smaller or bigger buttons in your control.

Design and extensibility

On the contrary to what you might expect, the control has been implemented as a regular usercontrol instead of a control contrainer. The buttons on the control are therefore not usercontrols in themselves, but simple (inner public) classes of the OutlookBar control (see diagram).

This makes implementing the control a lot easier. There is no need to cope with container interfaces that need to be implemented. Only the basic control events need to be captured, implemented, and overriden where needed.

The Paint event manages the drawing of the whole control. It will loop through the buttons currently in its Buttons collection and call the PaintButton method of each button like so:

private void OutlookBar_Paint(object sender, 
                           PaintEventArgs e)
    {
        int top = 1;
        foreach (OutlookBarButton b in Buttons)
        {
            b.PaintButton(e.Graphics, 1, top, 
                 b.Equals(this.selectedButton), false);
            top += b.Height+1;
        }
    }
Each Button will render itself on the control. The additional flags in the PaintButton method indicate whether a button should render itself as normal, selected, or highlighted.

Additionally, the MouseOver event is captured by the control, to highlight the button that the mouse is moving over. In order not to repaint the whole control and prevent flickering, only the buttons that change in appearance are repainted.

This also goes for the Click event. The SelectedButton property is set automatically, and the corresponding button will render itself as selected. The usercontrol overrides the Click event and implements a new click event according to the ButtonClickEventHandler. Because, we want to pass on an additional argument in the event indicating which button was pressed. For this purpose, the ButtonClickEventArgs class is implemented including a (readonly) Button property that can be used by the host application.

Future extensions would include:

  • Resize-handle as shown in Outlook which would allow you to resize the control. This can now be done with a splitter, however, visually it's just not as attractive.
  • Implement shortcut buttons on the lower part of the OutlookBar. When downsizing the control, the icons of each button is listed here and can be accessed by clicking on them (see Outlook).
  • Automatic calculation of the color schemes to use. I'm not sure how Outlook currently does this. If anyone has an idea on this, please let me know.

Apart from these extenstions, the control is pretty much complete.

Points of Interest

I learned the ease of use of implementing your own usercontrol in pure C# using the .NET Graphics libraries. This resulted in a well performing small reusable control with a very small footprint.

Additionally, I learned how to make the control implementation complete by allowing setting up the control at design time. Specifically, persisting the design time OutlookBar Buttons collection took me some time to figure out.

History

Version 1.0 of the OutlookBar usercontrol was written on 25th of May 2006. This article was published on the 26th of May 2006.

Version 1.1 released on 29th of May 2006 containing a major improvement to add buttons to the OutlookBar control at design time. Also the OutlookBar Buttons collection is now implemented based on the CollectionBase class to reduce lines of code.

Nov. 2nd 2006 - Thanks to Dave, a VB.NET version is available of the control for you to download.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Herre Kuijpers


Herre Kuijpers started working as a software developer at a small research company after completing his computer science degree in 1997, where he has gained a broad experience with developing and implementing computer systems in visual basic and visual c++.

Currently Herre Kuijpers works at Capgemini Netherlands for over 8 years, where he developed skills with all kinds of technologies and languages such as c#, asp.net, silverlight, VC++, Javascript, SQL, UML, RUP.
Currently he fulfills the role of software architect in various projects.
Occupation: Architect
Company: Capgemini
Location: Netherlands Netherlands

Other popular Menus articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 72 (Total in Forum: 72) (Refresh)FirstPrevNext
Generallink to formsmemberangels7776:09 2 Dec '08  
GeneralVisual Studio 2008memberMeyer Stiglingh3:31 4 Aug '08  
GeneralRe: Visual Studio 2008memberMycroft Holmes2:30 15 Oct '08  
GeneralProblem with Versionsmemberkolag23:16 12 Nov '07  
GeneralRe: Problem with VersionsmemberHerre Kuijpers9:35 13 Nov '07  
GeneralA helper method for selecting and highlighting buttons without using the mousememberPilsnerDk5:07 9 Nov '07  
Generalbar not visiblememberWestDev7:31 8 Oct '07  
GeneralRe: bar not visiblememberfc00590:51 9 Oct '07  
GeneralCan I make it as dockable to Outlook 2003memberptMujeeb0:30 18 Sep '07  
Generalbutton click not working...memberPaul Betteridge3:19 29 Aug '07  
GeneralI don't understand where the images are coming frommembersmesser19:11 22 Aug '07  
GeneralRe: I don't understand where the images are coming frommemberHerre Kuijpers21:59 22 Aug '07  
GeneralRe: I don't understand where the images are coming frommembersmesser3:40 23 Aug '07  
GeneralRe: I don't understand where the images are coming frommemberHerre Kuijpers9:35 23 Aug '07  
GeneralRe: I don't understand where the images are coming frommembersmesser9:57 23 Aug '07  
GeneralRe: I don't understand where the images are coming frommemberHerre Kuijpers10:24 23 Aug '07  
GeneralRe: I don't understand where the images are coming frommembersmesser10:43 23 Aug '07  
GeneralRe: I don't understand where the images are coming frommemberHerre Kuijpers0:00 24 Aug '07  
GeneralRe: I don't understand where the images are coming frommembersmesser4:17 24 Aug '07  
GeneralRe: I don't understand where the images are coming frommemberHerre Kuijpers1:46 25 Aug '07  
QuestionCan this code be used freely, such as projects in the workplace?memberbjswift17:33 6 Jul '07  
AnswerRe: Can this code be used freely, such as projects in the workplace?memberHerre Kuijpers1:58 7 Jul '07  
GeneralRe: Can this code be used freely, such as projects in the workplace? [modified]memberbjswift8:00 7 Jul '07  
GeneralRe: Can this code be used freely, such as projects in the workplace?memberHerre Kuijpers23:54 7 Jul '07  
GeneralHow to provide a list of resources for every outlookBarButtonmemberKapil Singhal1:38 19 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Nov 2006
Editor: Smitha Vijayan
Copyright 2006 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project