Click here to Skip to main content
15,896,453 members
Articles / Desktop Programming / Windows Forms

Creating a Custom DropDown Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (72 votes)
20 Jul 2010CPOL7 min read 209.8K   9.2K   228  
Explains how to effectively create virtually any type of dropdown control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Unity3.Controls
{
    public partial class ColorChooserControl : UserControl
    {
        UserControl curControl = null;

        private Color _color;
        public Color Color
        {
            get {return ((IColorPicker)curControl).Color;}
            set { ((IColorPicker)curControl).Color = value; }
        }

        public ColorChooserControl()
        {
            showControl(0); //custom picker
        }

        public ColorChooserControl(Color color)
        {
            InitializeComponent();
            _color = color;
            showControl(0); //custom picker

        }

        private void btnShowColorPicker_Click(object sender, EventArgs e)
        {
            if (btnShowColorPicker.Text == "Color Picker")
            {
                showControl(0);
            }
        }

        private void showControl(byte index)
        {
            if (curControl != null)
            {
                _color = ((IColorPicker)curControl).Color;
                this.Controls.Remove(curControl);
                curControl.Dispose();
                curControl = null;
            }
            switch (index)
            {
                case 0: //custom picker
                    curControl = new CustomColorPicker(_color);
                    break;
            }
            if (curControl == null)
                throw new ArgumentException("The specified color picker could not be loaded!");

            curControl.Bounds = new Rectangle(0, 0, 350, 270);
            this.Controls.Add(curControl);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Unity3 Software
United States United States
Richard Blythe is founder and CEO of Unity3 Software.
In his spare time he enjoys flying Cessna 172s, reading, playing his Taylor acoustic guitar and recording music. He's latest non-computer endeavor is to learn violin. (Ouch)

Comments and Discussions