Click here to Skip to main content
Click here to Skip to main content

Customizable ComboBox Drop-Down

By , 21 Jun 2009
 
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:

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 GNU Lesser General Public License (LGPLv3)

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+

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionContextMenuStrip on control in dropdownmemberwolfgang_hg26-Apr-13 4:03 
Questionautocomplete modememberalejandra004-Mar-13 0:42 
Questiondatasource no muestra informacionmemberalfred03-Oct-12 15:06 
GeneralMy vote of 5mvpKanasz Robert27-Sep-12 11:01 
QuestionFocus problemmembergeorgegarvasis7-Apr-12 1:24 
GeneralBinding DataGridviewmemberbbls198324-Mar-11 17:20 
GeneralMy vote of 5memberMark Guo1-Feb-11 16:10 
GeneralMsgBox message conflict with WndProcmemberhomerhuang3-Feb-10 16:41 
GeneralRe: MsgBox message conflict with WndProcmemberlhayes004-Feb-10 6:15 
GeneralDesigner improvement: SnapLinesmemberwolfgang_hg18-Nov-09 23:26 
GeneralRe: Designer improvement: SnapLinesmemberlhayes0025-Nov-09 8:18 
QuestionUse DroppedDown property instead of IsDroppedDown?memberT-C28-Oct-09 17:32 
AnswerRe: Use DroppedDown property instead of IsDroppedDown?memberlhayes0029-Oct-09 13:20 
Questionset combo textmemberjixiaoliang26-Sep-09 17:48 
AnswerRe: set combo textmemberlhayes0027-Sep-09 4:22 
Questionproperty ReadOnly [modified]membersiggi698-Sep-09 19:17 
AnswerRe: property ReadOnlymemberlhayes0011-Sep-09 2:32 
GeneralRe: property ReadOnlymemberlhayes0011-Sep-09 2:36 
GeneralControl.MinimumSizemembertonyt28-Aug-09 12:42 
GeneralRe: Control.MinimumSizememberlhayes0029-Aug-09 15:39 
GeneralRe: Control.MinimumSizemembertonyt7-May-10 2:22 
GeneralDropDown ListViewmemberxnuks17-Aug-09 1:42 
Questionautocompletemembercormoran29-Jul-09 5:19 
AnswerRe: autocompletememberlhayes007-Aug-09 12:33 
GeneralParent form losing focus on hidememberPoustic17-Jul-09 5:36 
GeneralRe: Parent form losing focus on hidememberlhayes0017-Jul-09 8:24 
GeneralRe: Parent form losing focus on hidememberPoustic17-Jul-09 9:05 
I tried various combinations without success... Here's the general idea:
 
private TreeView treeView1 = null;
 
private void Form1_Load(object sender, EventArgs e)
{
    treeView1 = new TreeView();
    treeView1.Nodes.Add("Node 1");
    treeView1.Nodes.Add("Node 2");
    treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
 
    customComboBox1.DropDownSizeMode = CustomComboBox.CustomComboBox.SizeMode.UseDropDownSize;
    customComboBox1.DropDownControl = treeView1;
}
 
void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    customComboBox1.HideDropDown();     // causes form to lose focus
    //customComboBox1.Parent.Focus(); 
}

GeneralRe: Parent form losing focus on hidememberlhayes0017-Jul-09 12:52 
GeneralRe: Parent form losing focus on hidememberPoustic21-Jul-09 6:45 
GeneralRe: Parent form losing focus on hidememberlhayes0021-Jul-09 7:31 
GeneralWrapping with toolstripmemberdebajyoti2005in8-Jul-09 23:21 
GeneralRe: Wrapping with toolstripmemberlhayes009-Jul-09 0:46 
GeneralDropDownClosed event not firingmemberdokmanov17-Jun-09 7:32 
GeneralRe: DropDownClosed event not firingmemberlhayes0017-Jun-09 9:27 
GeneralRe: DropDownClosed event not firingmemberdokmanov17-Jun-09 17:03 
GeneralRe: DropDownClosed event not firingmemberlhayes0019-Jun-09 3:13 
GeneralRe: DropDownClosed event not firingmemberdokmanov19-Jun-09 3:52 
GeneralRe: DropDownClosed event not firingmemberlhayes0019-Jun-09 6:28 
GeneralRe: DropDownClosed event not firingmemberdokmanov19-Jun-09 19:04 
GeneralRe: DropDownClosed event not firingmemberlhayes0019-Jun-09 21:40 
GeneralRe: DropDownClosed event not firingmemberdokmanov20-Jun-09 17:48 
GeneralRe: DropDownClosed event not firingmemberlhayes0021-Jun-09 2:31 
GeneralRe: DropDownClosed event not firingmemberlhayes0021-Jun-09 12:52 
GeneralRe: DropDownClosed event not firingmemberdebajyoti2005in8-Jul-09 23:34 
GeneralRe: DropDownClosed event not firingmemberlhayes009-Jul-09 0:41 
QuestionUsing a ListBox?membermetator25-Apr-09 9:33 
AnswerRe: Using a ListBox?memberlhayes0026-Apr-09 15:35 
GeneralRe: Using a ListBox?membermetator10-Jul-09 4:24 
GeneralRe: Using a ListBox?memberlhayes0010-Jul-09 14:55 
QuestionHow do I add this to my project pls?memberMember 197457016-Apr-09 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 21 Jun 2009
Article Copyright 2008 by Lea Hayes
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid