How to render a control to look like ComboBox with Visual Styles enabled?

.net, c#, winforms

Solution

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.

- VisualStyleRenderer class (MSDN)

- How to: Render a Visual Style Element (MSDN)

- 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.
}

Hope it helps!

Problem

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. 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). How can I get correct rendering of the control in a disabled state?

Original source