Click here to Skip to main content
6,595,854 members and growing! (19,554 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » ComboBox Controls     Intermediate License: The GNU Lesser General Public License

Customizable ComboBox Drop-Down

By lhayes00

A combobox control with a customizable drop-down
C# (C# 2.0), Windows, .NET (.NET 3.0), GDI+, Dev
Version:2 (See All)
Posted:22 Apr 2008
Updated:21 Jun 2009
Views:50,858
Bookmarked:99 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 6.28 Rating: 4.10 out of 5

1
2 votes, 5.9%
2
6 votes, 17.6%
3
10 votes, 29.4%
4
16 votes, 47.1%
5
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

About the Author

lhayes00


Member
I have been using computers since I was about 8 years old, and have always enjoyed studying new concepts and technologies. I have been programming since about 1995 and have become experienced with many programming languages and technologies including C/C++, C#, DirectX, OpenGL, Java, ASP.NET, XML, XSL, SQL, JavaScript, HTML, CSS, ActionScript, VB, and Conitec Game Studio A5 Pro.

I have always been interested in game and multimedia development and have recently graduated with the first-class degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich.

Since graduating I have been continuing to study software and game development. In particular I am interested in game engine creation and programming language development. In addition to study, I am working on the design and creation of a game engine.

In my spare time I enjoy playing my musical instruments, especially my guitars. Its great fun and something which helps me unwind after a long day. I enjoy many flavours of music, in particular rock and heavy metal bands like Megadeth, Anacrusis, and Pink Floyd.
Location: United Kingdom United Kingdom

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 65 (Total in Forum: 65) (Refresh)FirstPrevNext
QuestionUse DroppedDown property instead of IsDroppedDown? PinmemberT-C18:32 28 Oct '09  
AnswerRe: Use DroppedDown property instead of IsDroppedDown? Pinmemberlhayes0014:20 29 Oct '09  
Questionset combo text Pinmemberjixiaoliang18:48 26 Sep '09  
AnswerRe: set combo text Pinmemberlhayes005:22 27 Sep '09  
Questionproperty ReadOnly [modified] Pinmembersiggi6920:17 8 Sep '09  
AnswerRe: property ReadOnly Pinmemberlhayes003:32 11 Sep '09  
GeneralRe: property ReadOnly Pinmemberlhayes003:36 11 Sep '09  
GeneralControl.MinimumSize Pinmembertonyt13:42 28 Aug '09  
GeneralRe: Control.MinimumSize Pinmemberlhayes0016:39 29 Aug '09  
GeneralDropDown ListView Pinmemberxnuks2:42 17 Aug '09  
Questionautocomplete Pinmembercormoran6:19 29 Jul '09  
AnswerRe: autocomplete Pinmemberlhayes0013:33 7 Aug '09  
GeneralParent form losing focus on hide PinmemberPoustic6:36 17 Jul '09  
GeneralRe: Parent form losing focus on hide Pinmemberlhayes009:24 17 Jul '09  
GeneralRe: Parent form losing focus on hide PinmemberPoustic10:05 17 Jul '09  
GeneralRe: Parent form losing focus on hide Pinmemberlhayes0013:52 17 Jul '09  
GeneralRe: Parent form losing focus on hide PinmemberPoustic7:45 21 Jul '09  
GeneralRe: Parent form losing focus on hide Pinmemberlhayes008:31 21 Jul '09  
GeneralWrapping with toolstrip Pinmemberdebajyoti2005in0:21 9 Jul '09  
GeneralRe: Wrapping with toolstrip Pinmemberlhayes001:46 9 Jul '09  
GeneralDropDownClosed event not firing Pinmemberdokmanov8:32 17 Jun '09  
GeneralRe: DropDownClosed event not firing Pinmemberlhayes0010:27 17 Jun '09  
GeneralRe: DropDownClosed event not firing Pinmemberdokmanov18:03 17 Jun '09  
GeneralRe: DropDownClosed event not firing Pinmemberlhayes004:13 19 Jun '09  
GeneralRe: DropDownClosed event not firing Pinmemberdokmanov4:52 19 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jun 2009
Editor: Deeksha Shenoy
Copyright 2008 by lhayes00
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project