Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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?

like image 960
Peter Hession Avatar asked Aug 05 '08 21:08

Peter Hession


People also ask

What is ComboBox control in C#?

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down.

How do I make my ComboBox read only?

Just change the DropDownStyle to DropDownList . Or if you want it completely read only you can set Enabled = false , or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa.


1 Answers

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

Hope it helps!

like image 72
Patrik Svensson Avatar answered Sep 26 '22 08:09

Patrik Svensson