Click here to Skip to main content
11,412,833 members (76,413 online)
Click here to Skip to main content

Customizable ComboBox Drop-Down

, 20 Nov 2014 MIT
Rate this:
Please Sign up or sign in to vote.
A combobox control with a customizable drop-down

Sample Image

Introduction

This article presents an extension of the .NET ComboBox which provides custom drop-downs. The control can automatically add a resizer grip at the bottom of a drop-down.

Design

Custom drop-down functionality can be achieved using the .NET ToolStripDropDown and ToolStripControlHost classes. These two classes are great because they prevent application forms from becoming inactive during drop-down. The custom popup functionality required by this control has been encapsulated within the class PopupControl (a new addition since the original article posting).

In addition to facilitating this control, the PopupControl class can be used to provide drop-down support in your own controls. The PopupControl also integrates the drop-down resize support (now without the need for a nested resizable container, see below for details).

Previously the drop-down control was resizable by nesting a resizable container. This was an okay solution (because it worked) but it caused some redraw errors during resize operations. The resize functionality has now been significantly improved, and it is now also possible to define which sides of the drop-down are resizable (if any). Resizing can now also be done by dragging the resizable edges of the drop-down. The new resize functionality is achieved by intercepting Win32 hit testing messages, and responding appropriately based upon the set PopupResizeMode property.

The following UML class diagram provides an overview of the presented classes:

CustomDDComboBox/uml-overview.png

Using the Code

As with most controls, this control can be created dynamically at runtime, or by using the Visual C# IDE designer's drag and drop features. During runtime, the property DropDownControl must be assigned to an instance of another control. The assigned control must not be contained elsewhere as this will cause problems.

Most drop-down controls appear better when their borders are removed.

namespace CustomComboDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Dynamically created controls.

            // Create grid view control.
            DataGridView gridView = new DataGridView();
            gridView.BorderStyle = BorderStyle.None;
            gridView.Columns.Add("Column1", "Column 1");
            gridView.Columns.Add("Column2", "Column 2");
            gridView.Columns.Add("Column3", "Column 3");
            gridView.Columns.Add("Column4", "Column 4");
            gridView.Columns.Add("Column5", "Column 5");
            this.customComboBox1.DropDownControl = gridView;

            // Create user control.
            UserControl1 userControl = new UserControl1();
            userControl.BorderStyle = BorderStyle.None;
            this.customComboBox2.DropDownControl = userControl;

            // Create rich textbox control.
            RichTextBox richTextBox = new RichTextBox();
            richTextBox.BorderStyle = BorderStyle.None;
            this.customComboBox3.DropDownControl = richTextBox;
        }
    }
}

Points of Interest

When writing the code, I found that dropped-down controls were not retrieving immediate input focus. This problem occurs because even though the drop-down code is ignored during the Win32 message handler, the standard win32 drop-down still appears (1x1 pixels). To avoid this, a timer is created which forces the dropped-down control to be focused after the default win32 drop-down has been focused. Once the timer has done its job, it disables itself.

History

  • 22nd April, 2008: Original version posted
  • 29th April, 2008: Updated download files
    • Fixed a bug that was pointed out by Adam Hearn
  • 17th June, 2008: Various changes
    • Improved design
    • Odd drawing effects during drop-down resize have now been significantly improved
    • Added PopupControl for general custom drop-down support
    • Added download files: Version 2
  • 28th June, 2008: Updated download files for Version 2
  • 15th July, 2008: Updated to version 2.1.
    • Many thanks to member “Leon v Wyk” for this update
    • The improved version now hides incompatible properties from the Visual Studio .IDE properties panel which was causing confusion
  • 21st June, 2009: Updated to version 2.2 
    • Updated source, demo project and UML overview diagram
    • Fixed bug found by CodeProject member “dokmanov”
      Whilst the “DropDownClosed” event was being fired when the drop down arrow was used to close the drop down, it was not being fired when the drop down was closed by clicking in the form area

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Lea Hayes
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.

Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.

Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.

Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!

Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.

We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.

Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionNot passing selected value to combobox Pin
Uriel Tinashe Patsanza at 10-Dec-14 22:12
memberUriel Tinashe Patsanza10-Dec-14 22:12 
QuestionSnapshot Edit Tool Pin
Youzelin at 25-Nov-14 21:14
memberYouzelin25-Nov-14 21:14 
QuestionNice post Pin
Tridip Bhattacharjee at 13-Dec-13 0:08
memberTridip Bhattacharjee13-Dec-13 0:08 
QuestionMy Vote of 5 Pin
Serkank9494 at 26-Sep-13 7:52
memberSerkank949426-Sep-13 7:52 
QuestionContextMenuStrip on control in dropdown Pin
wolfgang_hg at 26-Apr-13 5:03
memberwolfgang_hg26-Apr-13 5:03 
Questionautocomplete mode Pin
alejandra00 at 4-Mar-13 1:42
memberalejandra004-Mar-13 1:42 
Questiondatasource no muestra informacion Pin
alfred0 at 3-Oct-12 16:06
memberalfred03-Oct-12 16:06 
SuggestionRe: datasource no muestra informacion Pin
Coarval COOP V at 24-Sep-14 23:52
memberCoarval COOP V24-Sep-14 23:52 
GeneralMy vote of 5 Pin
Kanasz Robert at 27-Sep-12 12:01
mvpKanasz Robert27-Sep-12 12:01 
QuestionFocus problem Pin
georgegarvasis at 7-Apr-12 2:24
membergeorgegarvasis7-Apr-12 2:24 
GeneralBinding DataGridview Pin
bbls1983 at 24-Mar-11 18:20
memberbbls198324-Mar-11 18:20 
GeneralMy vote of 5 Pin
Mark Guo at 1-Feb-11 17:10
memberMark Guo1-Feb-11 17:10 
GeneralMsgBox message conflict with WndProc Pin
homerhuang at 3-Feb-10 17:41
memberhomerhuang3-Feb-10 17:41 
GeneralRe: MsgBox message conflict with WndProc Pin
lhayes00 at 4-Feb-10 7:15
memberlhayes004-Feb-10 7:15 
GeneralDesigner improvement: SnapLines Pin
wolfgang_hg at 19-Nov-09 0:26
memberwolfgang_hg19-Nov-09 0:26 
GeneralRe: Designer improvement: SnapLines Pin
lhayes00 at 25-Nov-09 9:18
memberlhayes0025-Nov-09 9:18 
Hi,

Thanks, I am glad that this has been of use!

I hadn't implemented any of the designer specific features because they were beyond the scope of the project that I was working on. Thank you for providing a code example for "Snap lines". I will try and include it in the next version.

Thanks again!
Lea Hayes
QuestionUse DroppedDown property instead of IsDroppedDown? Pin
T-C at 28-Oct-09 18:32
memberT-C28-Oct-09 18:32 
AnswerRe: Use DroppedDown property instead of IsDroppedDown? Pin
lhayes00 at 29-Oct-09 14:20
memberlhayes0029-Oct-09 14:20 
Questionset combo text Pin
jixiaoliang at 26-Sep-09 18:48
memberjixiaoliang26-Sep-09 18:48 
AnswerRe: set combo text Pin
lhayes00 at 27-Sep-09 5:22
memberlhayes0027-Sep-09 5:22 
Questionproperty ReadOnly [modified] Pin
siggi69 at 8-Sep-09 20:17
membersiggi698-Sep-09 20:17 
AnswerRe: property ReadOnly Pin
lhayes00 at 11-Sep-09 3:32
memberlhayes0011-Sep-09 3:32 
GeneralRe: property ReadOnly Pin
lhayes00 at 11-Sep-09 3:36
memberlhayes0011-Sep-09 3:36 
GeneralControl.MinimumSize Pin
tonyt at 28-Aug-09 13:42
membertonyt28-Aug-09 13:42 
GeneralRe: Control.MinimumSize Pin
lhayes00 at 29-Aug-09 16:39
memberlhayes0029-Aug-09 16:39 
GeneralRe: Control.MinimumSize Pin
tonyt at 7-May-10 3:22
membertonyt7-May-10 3:22 
GeneralDropDown ListView Pin
xnuks at 17-Aug-09 2:42
memberxnuks17-Aug-09 2:42 
Questionautocomplete Pin
cormoran at 29-Jul-09 6:19
membercormoran29-Jul-09 6:19 
AnswerRe: autocomplete Pin
lhayes00 at 7-Aug-09 13:33
memberlhayes007-Aug-09 13:33 
GeneralParent form losing focus on hide Pin
Poustic at 17-Jul-09 6:36
memberPoustic17-Jul-09 6:36 
GeneralRe: Parent form losing focus on hide Pin
lhayes00 at 17-Jul-09 9:24
memberlhayes0017-Jul-09 9:24 
GeneralRe: Parent form losing focus on hide Pin
Poustic at 17-Jul-09 10:05
memberPoustic17-Jul-09 10:05 
GeneralRe: Parent form losing focus on hide Pin
lhayes00 at 17-Jul-09 13:52
memberlhayes0017-Jul-09 13:52 
GeneralRe: Parent form losing focus on hide Pin
Poustic at 21-Jul-09 7:45
memberPoustic21-Jul-09 7:45 
GeneralRe: Parent form losing focus on hide Pin
lhayes00 at 21-Jul-09 8:31
memberlhayes0021-Jul-09 8:31 
GeneralWrapping with toolstrip Pin
debajyoti2005in at 9-Jul-09 0:21
memberdebajyoti2005in9-Jul-09 0:21 
GeneralRe: Wrapping with toolstrip Pin
lhayes00 at 9-Jul-09 1:46
memberlhayes009-Jul-09 1:46 
GeneralDropDownClosed event not firing Pin
dokmanov at 17-Jun-09 8:32
memberdokmanov17-Jun-09 8:32 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 17-Jun-09 10:27
memberlhayes0017-Jun-09 10:27 
GeneralRe: DropDownClosed event not firing Pin
dokmanov at 17-Jun-09 18:03
memberdokmanov17-Jun-09 18:03 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 19-Jun-09 4:13
memberlhayes0019-Jun-09 4:13 
GeneralRe: DropDownClosed event not firing Pin
dokmanov at 19-Jun-09 4:52
memberdokmanov19-Jun-09 4:52 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 19-Jun-09 7:28
memberlhayes0019-Jun-09 7:28 
GeneralRe: DropDownClosed event not firing Pin
dokmanov at 19-Jun-09 20:04
memberdokmanov19-Jun-09 20:04 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 19-Jun-09 22:40
memberlhayes0019-Jun-09 22:40 
GeneralRe: DropDownClosed event not firing Pin
dokmanov at 20-Jun-09 18:48
memberdokmanov20-Jun-09 18:48 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 21-Jun-09 3:31
memberlhayes0021-Jun-09 3:31 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 21-Jun-09 13:52
memberlhayes0021-Jun-09 13:52 
GeneralRe: DropDownClosed event not firing Pin
debajyoti2005in at 9-Jul-09 0:34
memberdebajyoti2005in9-Jul-09 0:34 
GeneralRe: DropDownClosed event not firing Pin
lhayes00 at 9-Jul-09 1:41
memberlhayes009-Jul-09 1:41 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 20 Nov 2014
Article Copyright 2008 by Lea Hayes
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid