Owner Draw ComboBox
| Download : Source Code |
|
To do some custom drawing and painting with any of the windows controls, you need to do the drawing of the control yourself. The "OwnerDraw" property of the control will allow you to accomplish this. In this article we will try to custom draw a ComboBox..
|
| Step 1: Create a simple windows form project and add a combo box from the windows control toolbox into the form. |
| In the properties for the combobox set the DrawMode option to "OwnerDrawFixed". |
|
When the item is owner drawn , the application will send the DrawItem & MeasureItem event to allow us to do all the custom drawing. private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)Forevery item that needs to be drawn the DrawItemEventArgs parameter willcontain the index of the item to draw. It also contains the graphicsobject that we will use to do all the drawing and painting. Here is asimple example ( The Font Combo Box ) |
| private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Graphics g = e.Graphics ; Rectangle r = e.Bounds ; if ( e.Index >= 0 ) Rectangle rt = r ; SolidBrush b = (SolidBrush)colorArray[e.Index]; Console.WriteLine(e.State.ToString()); |
| For a OwnerDrawFixed property the MeasureItem function will have no effect. This function will be activated only for the OwnerDrawVariable item. |














No comments yet... Be the first to leave a reply!