Click here to Skip to main content
Licence CPOL
First Posted 11 May 2011
Views 6,505
Downloads 329
Bookmarked 9 times

Creating a simple ButtonDropdown Control

By | 3 Jun 2011 | Article
How to create a simple ButtonDropdown control

Introduction

This post helps to create a ButtonDropDown in Windows Form (I was not able to find a control in Visual Studio for my requirement, so I made one and I’m not sure whether there is one already). For this, I’m using a combination of Button control and ContextMenuStrip control.

Image01.jpg

The arrow in the button is an image with middle-right aligned.

  1. Initialize a ContextMenuStrip with the required MenuItems
  2. Register the click events of ContextMenuStripMenuItems
  3. Pop out the context menu on Button click with the given x, y axis (w.r.t to the button), y axis value should be the height of the button:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ButtonDropDownTest
{
    public partial class Form1 : Form
    {
        private ContextMenuStrip _contextMenuAutoFill;

        public Form1()
        {
            InitializeComponent();
            CreateContextMenuStrip();
        }

        private void CreateContextMenuStrip()
        {
            _contextMenuAutoFill = new ContextMenuStrip();
            _contextMenuAutoFill.Items.Add("Dropdown item 1");
            _contextMenuAutoFill.Items.Add("Dropdown item 2");
            _contextMenuAutoFill.Items.Add("Dropdown item 3");
            _contextMenuAutoFill.Items.Add("Dropdown item 4");

            foreach (ToolStripMenuItem mItem in _contextMenuAutoFill.Items)
                mItem.Click += 
                new System.EventHandler(this.AutoFillToolStripMenuItem_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.ContextMenuStrip = _contextMenuAutoFill;
            button1.ContextMenuStrip.Show
		(button1, new System.Drawing.Point(0, button1.Height));
        }

        private void AutoFillToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string m = ((ToolStripMenuItem)sender).Text;
            MessageBox.Show("You have clicked '" + m + "'");
        }
    }
}

This link explains how to create this control using custom control.

License

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

About the Author

Balu Sathish

Software Developer
Calpine Technologies
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembersolowilliam3:09 6 Aug '11  
GeneralMy vote of 3 PinmemberJason Vogel10:28 12 May '11  
GeneralIdea is good, need to be better PinmemberMickeyWu4:12 12 May '11  
GeneralMy vote of 1 PinmemberHossein Montazeri5:50 11 May '11  
QuestionRe: My vote of 1 PinmemberWarrick Procter20:08 11 May '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 3 Jun 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid