tags:

views:

1567

answers:

4

I have a control that is modelled on a ComboBox. I want to render the control so that the control border looks like that of a standard Windows ComboBox. Specifically, I have followed the MSDN documentation and all the rendering of the control is correct except for rendering when the control is disabled.

Just to be clear, this is for a system with Visual Styles enabled. Also, all parts of the control render properly except the border around a disabled control, which does not match the disabled ComboBox border colour.

How can I get correct rendering of the control in a disabled state?

A: 

Hi Peter,

Could you elaborate a little more on what's going on with rendering while the ComboBox is in a disabled state?

Bryan Roth
A: 

Are any of the ControlPaint methods useful for this? That's what I usually use for custom-rendered controls.

Nick
+3  A: 

I'm not 100% sure if this is what you are looking for but you should check out the VisualStyleRenderer in the System.Windows.Forms.VisualStyles-namespace.

  1. VisualStyleRenderer class (MSDN)
  2. How to: Render a Visual Style Element (MSDN)
  3. VisualStyleElement.ComboBox.DropDownButton.Disabled (MSDN)

Since VisualStyleRenderer won't work if the user don't have visual styles enabled (he/she might be running 'classic mode' or an operative system prior to Windows XP) you should always have a fallback to the ControlPaint class.

// Create the renderer.
if (VisualStyleInformation.IsSupportedByOS 
    && VisualStyleInformation.IsEnabledByUser) 
{
    renderer = new VisualStyleRenderer(
        VisualStyleElement.ComboBox.DropDownButton.Disabled);
}

and then do like this when drawing:

if(renderer != null)
{
    // Use visual style renderer.
}
else
{
    // Use ControlPaint renderer.
}
  • List item

Hope it helps!

Patrik
A: 

Hi,

Thanks to everyone for their answers and apologies for not being able to reply sooner because I have been away on holiday!

I can be a bit more specific on the details of my question: I am using the VisualStyleRenderer class. MSDN suggests using the VisualStyleElement.TextBox element for the TextBox part of the ComboBox control but a standard disabled TextBox and a standard disabled ComboBox draw slightly differently (one has a light grey border, the other a light blue border).

Obviously I'm being very picky but I was wondering whether there's something in the documentation that I'm missing?

Peter Hession