Combobox heading

G

Guest

I have an owner drawn combobox with multiple columns. I would like to
implement headings for the columns. I have attempted to do this within the
DrawItem event, but there is no space, I can not draw outside the region, and
if I move where the items are drawn (down from e.Bounds.Y) to make space, the
selection bar no longer lines up. I would appreciate any suggestions about
how to handle this. Is there a better event to be drawing the headings in?
Does anyone have code for a combobox with headings? Thanks.
 
P

Peter Huang [MSFT]

Hi Richard,

Based on my research, ComboBox owner draw only expose two events for us to
customize, it did not provide a heading customize feature.
As a workaround I think we may try to set the ItemHeight to double size, so
that we can draw a single size heading and single size text.
e.g.
private void comboBox1_MeasureItem(object sender,
MeasureItemEventArgs e)
{
if (e.Index == 0)

e.ItemHeight = 40;
else
e.ItemHeight = 20;
e.ItemWidth = 260;
}

switch (e.Index)
{
case 0:
size = 15;
animalColor = System.Drawing.Color.Gray;
family = FontFamily.GenericSansSerif;
// Draw the background of the item.
e.DrawBackground();

// Create a square filled with the animals color. Vary
the size
// of the rectangle based on the length of the animals
name.
rectangle = new Rectangle(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height / 2);
e.Graphics.FillRectangle(new SolidBrush(animalColor),
rectangle);

// Draw each string in the array, using a different
size, color,
// and font for each item.
myFont = new Font(family, size, FontStyle.Bold);

e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), myFont,
System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X, e.Bounds.Y+
e.Bounds.Height/2 , e.Bounds.Width, e.Bounds.Height/2));
// Draw the focus rectangle if the mouse hovers over an
item.
e.DrawFocusRectangle();
break;


NOTE: the code snippet above is just for demo only.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Richard,

You are welcomed!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top