Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-08
15
Medium Priority
?
996 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 12

Assisted Solution

by:Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano earned 200 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 37823244
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 800 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 70

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 70

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 800 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

721 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