Is it possible to change the backcolor of disabled controls in VB.NET?

After disabling certain controls such as e.g. TextBoxBase controls, the backcolor stays white (or a color very close to white) when disabling them. User may not be able to easily identify if such controls are actually disabled so I would like to change the "disabled backcolor" in some way. Is this possible? I no color changes seem to be applied when the controls are disabled.
andreas_rafnAsked:
Who is Participating?
 
Alfredo Luis Torres SerranoConnect With a Mentor Development Director / DBACommented:
TextBox1.Enabled = False
TextBox1.BackColor = Color.White

This will cause the BackGround to be white, but with a Grey text in it (on my Color Scheme).
0
 
andreas_rafnAuthor Commented:
On my Windows 7 the background is already white when the control is disabled. Thus when no input/text (which would then be gray) is in the control, there is no way to see if it is actually disabled, which is my problem.

When using the backcolor property, say setting it to green, the control will still have a white background when it is disabled. Only when I enable the control, the green backcolor is visible. So it is like the disabling overrides the backcolor (and forecolor) set.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
andreas_rafnAuthor Commented:
This is how two disabled controls look like, as you can see it's difficult for the user to see if they are disabled or not:

Two disabled controls
0
 
andreas_rafnAuthor Commented:
Maybe I forgot to mention that I'm woirking with Windows.Forms.Control type VB.NET controls and not browser controls.
0
 
basicinstinctCommented:
oh sorry
0
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Simply react to the Enabled changed property of the control:
Private Sub TextBox1_EnabledChanged(sender As Object, e As System.EventArgs) Handles TextBox1.EnabledChanged

   If TextBox1.Enabled Then
     TextBox1.BackColor = Control.DefaultBackColor
   Else
     TextBox1.BackColor = Color.DarkGray
   End If

 End Sub

Open in new window

Play with different colors until you find one that fits the bill. On your dark blue form, a light blue BackColor might be a good indication.

You might also play with the associated Label Forecolor and make it a different color than the other lables when its TextBox is disabled.
0
 
andreas_rafnAuthor Commented:
I must have done something wrong the first time around with the TextBoxBase controls, as it works now.

However the problem persists for combobox controls (BackColor changes will not show when set after disabling) as well as checkbox and radiobutton controls (ForeColor changes will not show when set after disabling).

I need a consistent disabled look for all these controls.
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you checked my article?
0
 
andreas_rafnAuthor Commented:
Yes, but I was hoping for a simpler solution I must admit
0
 
Éric MoreauSenior .Net ConsultantCommented:
As you found, the controls are not natively supporting that feature. To be able to properly support it, it is not enough to simply change the color, you have to circumvent some other events
0
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Have you considered working on the ForeColor of the accompanying Label? This is quite evident.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
You could overlay a Label over the control when it is disabled. When the ComboBox (or other control) receives the focus, remove the overlay. When the overlay received the focus, remove it and set the focus to the ComboBox.

You would have complete control of the color on that one.
0
 
andreas_rafnAuthor Commented:
Thanks for all the suggestions, I love EE! :) Wow, sometimes something one would think ought to be simple, just isn't. To the lack of this feature in .NET is a mystery as it makes control handling in applications with non-standard Windows UI design (e.g. dark background) quite cumbersome.  

Actually I thought of the acommpanying label too, and ended up using that approach for simplicity. I wrapped it in a generalized LabelledInputControl class handling different types of controls in different ways. For example, my solution for CheckButton's and RadioButtons was keeping these enabled at all time, since disabling them made their text dark grey and impossible to read on my background, and then setting their properties as follows (maybe it can act as inspiration for other with the same issue):

...
        If TypeOf _Control Is RadioButton Then
            With DirectCast(_Control, RadioButton)
                If Me.Enabled Then
                    .TabStop = True
                    .AutoCheck = True
                    .FlatStyle = FlatStyle.Standard
                    .ForeColor = ApplicationColor.ControlHeaderEnabledText
                Else
                    .TabStop = False
                    .AutoCheck = False
                    .FlatStyle = FlatStyle.Flat
                    .ForeColor = ApplicationColor.ControlHeaderDisabledText
                End If
            End With
        ElseIf TypeOf _Control Is CheckBox Then
            With DirectCast(_Control, CheckBox)
                If Me.Enabled Then
                    .TabStop = True
                    .AutoCheck = True
                    .FlatStyle = FlatStyle.Standard
                    .ForeColor = ApplicationColor.ControlHeaderEnabledText
                Else
                    .TabStop = False
                    .AutoCheck = False
                    .FlatStyle = FlatStyle.Flat
                    .ForeColor = ApplicationColor.ControlHeaderDisabledText
                End If
            End With
        Else
...

Note that Me.Enabled is a separate Enabled flag in the LabelledInputControl made necessary since the Enabled property of CheckBoxes and RadioButtons would otherwise be wrong since the Enabled property of these types of objects are forced to Enabled=true using event handling.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.