Android Addicted Apps Creator

May 14, 2009

Custom Combo Box with colors and text – Part II

Filed under: Windows forms — alinberce @ 17:39
Tags: ,

(Warning: for bored people, who are too tired to read all this bla bla, just go to the end of the post for the video)

         Yesterday we did pretty cool things with the ComboBox control. Today we’ll enhance it a little bit more. The previous solution works great if you don’t need many ComboBoxes. But if you do, than it would be uncomfortable to write DrawItem methods for each one. To get rid of this issue, we’ll create a custom ComboBox which will do the job for us. Sounds nice ?

Add a new class to the project and name it CboColors.cs. This will contain a custom ComboBox control with it’s overridden methods, in order to spare us from doing the same things over and over again.

Here is the code:

using System.Drawing;
using System.Windows.Forms;

namespace qPlanner.UserControls
    public partial class cboColored : ComboBox
        protected override void OnCreateControl()

            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.MaxDropDownItems = 11;

        protected override void OnDrawItem(DrawItemEventArgs e)

            if (e.Index >= 0)
                object current = this.Items[e.Index];
                object currentValue = current.GetType().GetProperty(this.ValueMember).GetGetMethod().Invoke(current, null);
                object currentDisplay = current.GetType().GetProperty(this.DisplayMember).GetValue(current, null);
                Color clr = currentValue is Color ? (Color)currentValue : Color.Black;
                using (Brush brush = new SolidBrush(clr))
                    e.Graphics.FillRectangle(brush, e.Bounds.X + 1, e.Bounds.Y + 1, 13, 13);
                using (Brush brush = new SolidBrush(e.ForeColor))
                    e.Graphics.DrawString(currentDisplay.ToString(), e.Font, brush, e.Bounds.Left + 16, e.Bounds.Top);


The changes from the previous part are not very obvious, but they make the difference. As you can see, now I won’t take the information about Color and Name from the ComboBox’s DataSource but from the current drawn item itself.

Build the solution. Now get back to the Form1 in Design mode. In the ToolBox there should be a brand new item named cboColored.


Drag this item into the form and rename it to cboColored. The only thing that remains to do is give this custom ComboBox a DataSource.

private void GetPriorityList()
            lstPriority = PriorityCollections.GetPriorities();
            cboPriority.DataSource = lstPriority;
            cboPriority.ValueMember = "Color";
            cboPriority.DisplayMember = "Name";
          //added for the custom combo
            cboColored.DataSource = lstPriority;
            cboColored.ValueMember = "Color";
            cboColored.DisplayMember = "Name";


Run the project and it should look like this:


Wow. It works. It’s that simple, just add a DataSource, ValueMember and DisplayMember and you’re done. Happy coding.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: