Solved

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

Posted on 2012-04-08
15
778 Views
Last Modified: 2012-04-09
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.
0
Comment
Question by:andreas_rafn
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 12

Assisted Solution

by:Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano earned 50 total points
ID: 37821461
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
 

Author Comment

by:andreas_rafn
ID: 37821669
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
 

Author Comment

by:andreas_rafn
ID: 37821684
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
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37821788
0
 

Author Comment

by:andreas_rafn
ID: 37821898
Maybe I forgot to mention that I'm woirking with Windows.Forms.Control type VB.NET controls and not browser controls.
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37821906
oh sorry
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 37823244
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 total points
ID: 37823595
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
 

Author Comment

by:andreas_rafn
ID: 37823741
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37823758
have you checked my article?
0
 

Author Comment

by:andreas_rafn
ID: 37823808
Yes, but I was hoping for a simpler solution I must admit
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37823844
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
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 total points
ID: 37823925
Have you considered working on the ForeColor of the accompanying Label? This is quite evident.
0
 
LVL 40
ID: 37823970
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
 

Author Closing Comment

by:andreas_rafn
ID: 37824498
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now