Extending the ComboBox with C#

Ever needed to do something with the ComboBox, but found that the normal ComboBox is quite limited? The ComboBox, as we know it, shows a list of items. What if you wanted to show colours, fonts, drawn lines, or images with the ComboBox? Wouldn't that be nice? Yes, it would. Not only that, but why not change the appearance and normal behaviour of the normal ComboBox? In this article, I will demonstrate how to do all these and more with the ComboBox.

Your Project: CrazyCombos

As the name implies, you are really going to go a bit crazy with extending the ComboBox's uses.

Create a new Windows Forms project, named CazyCombos.

Fonts Combo

The first thing that I will cover is creating a Font ComboBox. A Font ComboBox displays a list of all the available fonts on the user's PC, similar to what you use to change the Font in MS Word or MS Excel, for example. Apart from listing the available fonts, you also can display a small sample of each particular font by writing the current font name with the current font. For example, if you want to display Comic Sans MS, you must use the Comic Sans MS font to write Comic sans MS; in other words, the name of the font.

Go to frmCrazy's Design view and add a ComboBox named cboFontsCrazy to it. Set cboFontsCrazy's DrawMode property to OwnerDrawVariable. This enables you to display custom items in the list. Add the following variable to frmCrazy:

Here, you created an array to store all the system's fonts, and then you looped through all of them. As you loop, you set each font's style to Bold, so that it appears Bold in FontCbo. The interesting thing here to note is that not all of the fonts support the Bold style, not even the Regular font style. What I had to do was to wait for the program to give me errors on the particular font(s) name(s). On each of the fonts in the if block, I received an error that they don't support the regular or Bold styles. This meant that each of those fonts only support italic; that is why I had to identify them, tand hen change their styles to italic. You could have set the style just to italic in the first place, but still you may have received errors on fonts not supporting Bold. So, it is basically trial and error.

The physical drawing of each font happens in the cboFontsCrazy_DrawItem event:

Extending the ComboBox with C#

Displaying Different Drawn Lines in a ComboBox

If you have seen the Underline Styles ComboBox in MS Word, you know that it displays various line drawings to be used as underlines. You will attempt this next, although it won't be as advanced as the Line ComboBox from MS Word, it stukk will give you the platform to work from. The first step is to add a new class, called LineCbo.cs, to your project.

Make sure you have all the necessary NameSpaces in your class:

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

You need to create an array to store all available Dash styles; you get this from System.Drawing.Drawing2D.DashStyles. This will give you dashed, dotted, and thick lines. Then, you create a variable to be used just in a loop to loop through each dash style.

The LCFillLineTypes procedure simply loops through the DashStyles enumeration, and adds each item found to the list. Without the aid of overriding the OnDrawItem event, the actual showing of the lines will not happen. Add the following to your class:

In the OnDrawItem event, you created a Rectangle to draw onto, and also a Pen to draw with. You loop through the DashStyle array, and as you loop, you cast the current found dash style so that you can set the Pen's DrawStyle property to the correct current dashstyle present in the array. After you have cast the pen, you simply draw the line with the current dashstyle. I have opted to continue looping through the arrays to keep drawing as needed; you can, of course, opt not to.

Build your program.

Once built (and without errors), you will notice that the LineCbo component was added to your toolbox as shown in Figure 2. You can now add it from the toolbox, to your form and name it lineCboCrazy, or anything else descriptive. If you run your program now, you will find that the LineComboBox functions as intended, as shown in Figure 3.

As with LineCbo, you need to override the OnDrawItem event, so that, in this case, you will be able to display pictures inside the ComboBox, just as a normal ImageComboBox. You have to establish whether or not you have decided to include an image with the item text. If you are displaying text as well as pictures, you need to draw the picture, and then write the text. If you only want to display text, you simply have to draw the text. You can, of course, change the colours of the Item text and make the text Bold or not. You are only half way now, because you still need a class for your ImageCombo's text. Inside ImgCbo, create another class called ICItem. This class will be used to display the actual text and picture(s) in a format you want. Add the following variables to ICItem:

If you can remember, in the ImgCbo's OnDrawItem event you established whether or not you want pictures along with text. This decision you create via the use of ICItem's constructor. You can overload it to display text only, to display text and the picture, to be able to change the text to bold, and the forecolour of the current item. Add the following:

Once built, the ImgCbo component will appear inside your Toolbox. Add this new component to your form and name it imgCboCrazy. You need to add the physical items to your new component in frmCrazy_Load:

In frmCrazy_Load, you simply got a list of all available colours. In the DrawItem event, you gave each item a background colour of the current colour, and wrote the current colour's name in the middle.

Extending the ComboBox with C#

Aligning ComoBox Objects

If you look at a ComboBox, you will notice that it consists of a List, a Drop down button, and optionally a Scrollbar. Wouldn't it be nice to be able to move these parts around? You can play around with it now. Add a new class named ComboAlignSettings.cs, and add the following NameSpace:

As you probably know, the System.Runtime.InteropServices NameSpace means only one thing, and that is APIs. Without the use of the Windows API moving the ComboBox Scrollbar to the left, right-aligning the text, and moving the ComboBox, a dropdown button would not be possible. The API helps you get an appropriate handle to the ComboBox "control" you want to move, and then move it. Add all the APIs, Constants, and Structures needed:

In the constructor, I got a Handle to this component, with the use of the CASGetHandle method (which is covered later), and I set the default alignment options for this component. The drop-down button is displayed on the right, the scrollbar is right, and the text is aligned left, like an ordinary ComboBox.

Build your project, and once successfully built, your component should appear in the Toolbox, as displayed in Figure 2.

Making the Alignment ComboBox Work

On your form, add this new component and give it a proper name, such as cboAlignAllCrazy, and add three buttons as well. These buttons will be used to align each part of your custom ComboBox you've just added. Once you have added all these controls, you can switch to code view. Currently, cboAlignAllCrazy does not contain any items, so quickly add some items in frmCrazy_Load:

Top White Papers and Webcasts

This white paper examines the economics of deploying Red Hat's Storage Server. Based on GlusterFS, a distributed file system that Red Hat acquired as part of Gluster, Red Hat Storage Server is ushering in a new era of software-based storage (also known as software-defined storage by many suppliers) solutions. Such solutions leverage commodity x86-based hardware from server vendors and a distributed shared nothing architecture that allows businesses to build out a service-based storage infrastructure in an …

The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals:
Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals.
IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices.
Job satisfaction is the No. 1 predictor of performance against organizational …