Click here to Skip to main content
13,597,064 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 30 May 2006
Licenced CPOL

ButtonMenu - a.k.a. A Simple Hack For A Nice GUI

, 6 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Developing a nice menu look using only C#
Sample Image - ButtonMenu.jpg


One of my favourite desktop gadgets is/was Google Deskbar, but because Google staff decided to stop its development, I've decide to build up a clone of it.

Looking at its GUI, the first problem that emerges is the toggle like button for menu displaying. I've Googled a lot seeking a similar if not equal component but without success, so I've started to think about the way that I must follow to develop a component with the same look and feel.

A first choice was, naturally, to use a ToggleButton but also this component is not directly available in .NET, but only as a COM component. Using a COM component can, if not needed, reduce the cleanness of the code. Some tests have revealed that a component with a toggle-like feel is available as a special appearance of a checkbox. So we can start coding...

The Code

public partial class ButtonMenu : CheckBox

In the constructor, I set up basic look oriented properties:

this.Appearance = Appearance.Button;
this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.FlatStyle = FlatStyle.Popup;

Using CheckBox as base class grants me the ability to reduce the code I need to write to reach my goal. The only code I need to add will be menu-specific.... Well put in this menu!

private ContextMenuStrip menu=null;
public ContextMenuStrip Menu
        return menu;
         * If menu != null then we have a menu to clean before to change
            menu.Closed -= menu_Closed;
        menu = value;
        if(menu != null)
            menu.Closed += menu_Closed;

Using a property gives me two advantages:

  1. I can change it at design time
  2. I can administer in a simple way the adding and the removing of event handler

In the precedent code, we have introduced the method menu_Closed. It's something like:

private bool frombutton = false;
void menu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
     * Popup Menu closing... raise the button
        Checked = false;

The boolean flag frombutton is used to check the reason of menu closing, in details it's true if menu has been closed due to a button event and it's false if it's closed by focus lost. The main method is simple... after you have understood the behavior of the Show method:

private void DisplayMenu()
    // No menu...
    if (menu == null)
    Point offset = new Point();
    switch (direction)
        case ToolStripDropDownDirection.AboveLeft:
            offset.X = menu.Width;
            offset.Y = 0;
        case ToolStripDropDownDirection.AboveRight:
            offset.X = this.Width-menu.Width;
            offset.Y = 0;
        case ToolStripDropDownDirection.BelowLeft:
            offset.X = menu.Width;
            offset.Y = this.Height;
        case ToolStripDropDownDirection.BelowRight:
            offset.X = this.Width - menu.Width;
            offset.Y = this.Height;
        case ToolStripDropDownDirection.Default:
            offset.X = (this.Width - menu.Width)/2;
            offset.Y = this.Height;
        case ToolStripDropDownDirection.Left:
            offset.X = 0;
            offset.Y = 0;
        case ToolStripDropDownDirection.Right:
            offset.X = this.Width;
            offset.Y = 0;
    menu.Show(this, offset, direction);

In the code above, direction is a value in ToolStripDropDownDirection enumeration accessible with a standard get/set pair.


As code, also the instructions are really simple, after you've added ButtonMenu component to toolbox, drag it on your form and set Menu property from property page (or via code). Enjoy. EDIT: I've merged code for autoplacing written by BlackTigerAP (I've done a little "case compression" to reduce code replication. I've added a new property public ButtonMenuStyle Style to switch between flat and button styles from design. Of course:

public enum ButtonMenuStyle
  Flat = 0,
  Button = 1,


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


About the Author

Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

General.Net 1.1 [modified] Pin
zohre_j14-Jul-06 19:06
memberzohre_j14-Jul-06 19:06 
I have tried to make your code work in .net 1.1 but there is no toolStripMenuItem.popDownDirection (I'm not sure i have written it correctly)there, also the menu doesn't have a width or length,so I don't know what to do! Can you help me?

-- modified at 1:09 Saturday 15th July, 2006
GeneralRe: .Net 1.1 Pin
Ben3.Tyo23-Jan-08 16:15
memberBen3.Tyo23-Jan-08 16:15 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180621.3 | Last Updated 7 Jun 2006
Article Copyright 2006 by lbenini
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid