Inactive control background color
Asked Answered
P

2

6

Is there a way on Windows to retrieve the color used as background color for inactive controls (TextBox, etc.)? Or better yet, the border color too?

This is for Windows Forms and I haven't been able to find anything suitable in SystemColors. There is no such thing

Case in point. I have a text box which may not be large enough for the text it holds and it is disabled. When it is disabled the user cannot scroll to view the entire text and I can't even display a tooltip for obvious reasons.

So what I've done now is setting the TextBox's ReadOnly property to true which allows me to display tooltips and have the control scrollable. The client now wants the text box to look like it was disabled; ReadOnly is a pretty nasty property since it still looks like it can be edited. So I thought putting the proper background color in there might be enough to fool most users. I can't use an arbitrary gray value since there are other disabled controls on that form as well and color differences would probably be noted. So is there a way I can find out how a disabled control gets rendered? Background color and border color or at least the former should really be enough here but I'd rather not guess. Platforms in question are most likely XP and Vista both maybe with or without themes.

ETA: Disregard. The question was stupid and an error on my behalf I should have spotted earlier. It was just a little weird that a single TextBox wouldn't adhere to a gray background.

Pulsometer answered 10/12, 2009 at 16:39 Comment(5)
I think that it is rather established that textboxes with "gray" (as in SystemColors.Control) are not editable, so I would not really consider this a problem. But if the customer says so, well, then the customer says so.Eighty
Well, I didn't know which gray exactly. The fact that they still can focus the control with ReadOnly = true is probably unnoticeable to them. Thankfully :-)Pulsometer
I was about to say the same thing. Since read-only textboxes have the same background color as disabled textboxes, they DO look read-only if you ask me.Disembroil
Hm, did I do something horribly wrong? One specific text box on our interface still had a white background. That was the oddball one the client complained about. Trying again. Maybe the designer messed up some property assignment orders.Pulsometer
Ouch. Ok. Setting a specific background color overrides the one set by ReadOnly. Even if the background color was set before ReadOnly was set. And as it so happened, the background color was set to Window.Pulsometer
D
26

When disabled, the textbox has background color SystemColors.Control and foreground color SystemColors.GrayText.

Disembroil answered 10/12, 2009 at 16:44 Comment(0)
S
4

Try this:

        treeView1.EnabledChanged += (s, o) =>
            {
                treeView1.BackColor = treeView1.Enabled ? Color.White : SystemColors.Control;
            };
Selfcommand answered 10/9, 2012 at 1:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.