How do I change the font color that's seen on a disabled button?

Answered How do I change the font color that's seen on a disabled button?

  • Friday, September 07, 2012 9:20 AM
     
     

    Hello everyone,

    I have a problem with the design - namely the font color - of disabled buttons. I can barely see whether my button is enabled or disabled because the font is so similar. But if I use a dark button with a bright font color it's easy to see because the font color of the disabled button is always dark. So how can I change the font color of a disabled button.

    Thanks in advance!

All Replies

  • Sunday, September 09, 2012 8:38 AM
     
     Proposed Answer

    The button will always use System.GrayText to draw disbaled text, unless the button is not using Visual Styles, in which case it will draw etched text as in ControlPaint.DrawStringDisabled(). The only way to overcome this is to draw the button yourself.

    There are a couple of examples showing how to custom paint a button on my button tips page:
    http://dotnetrix.co.uk/button.htm


    Mick Doherty
    http://dotnetrix.co.uk
    http://glassui.codeplex.com

  • Tuesday, September 11, 2012 8:44 AM
     
     

    I now compared my custom button with the normal Win XP button and I saw that the disabled color isn't the same after all. So now I wish that Visual Styles WOULD use the same color for disabled buttons, but it doesn't do it. In the following image, both buttons are disabled. The left button uses a grey of 224 as BackColor and 64 as ForeColor, while the other one uses Control and ControlText as it is the standard of Visual Styles. The left button looks almost the same when enabled and if it would use the color of the right button I wouldn't have a problem.

    http://sdrv.ms/NlTiQM

    Also thank you for the tutorial on how to create my own button. I'll do that if I have no other option.


  • Tuesday, September 11, 2012 1:44 PM
     
     Proposed Answer Has Code

    Hi NeuerBarde,

    Please try this:

    ControlPaint.DrawStringDisabled(g, this.Text, this.Font, Color.Transparent, 
                    new Rectangle(CustomStringWidth, 5, StringSize2.Width, StringSize2.Height), StringFormat.GenericTypographic);


    Regards, http://www.shwetalodha.blogspot.in/

    • Proposed As Answer by Shweta Jain Tuesday, September 11, 2012 1:45 PM
    •  
  • Tuesday, September 11, 2012 5:16 PM
     
     

    I'm a little confused now !!!

    Is this a standard WindowsForms Button? If so then you should be seeing what is displayed on the right.

    You mention Visual Styles, but neither of the buttons in your image are using Visual Styles and so neither of them should be using Visual Styles Colors.

    As Shweta Jain say's, ControlPaint.DrawString() should render text as the in the button on the right, but the color specified should be the controls background color. ControlPaint will then use the two Colors produced by ControlPaint.Light() and ControlPaint.Dark() to draw the text (which should match two of the colors used on the 3d border of the button). On a button using the System Control Color as the backround the colors used should be equal to the two SystemColors ControlLight and ControlDark.


    Mick Doherty
    http://dotnetrix.co.uk
    http://glassui.codeplex.com

  • Wednesday, September 12, 2012 7:25 AM
     
      Has Code

    Ok, sorry, I was assuming that WinForms uses VisualStyles. Maybe I should look up the words I don't know. :-P

    Well, both buttons are standard C# WinForms buttons and I can make the right one look like the left one ONLY by changing BackColor and ForeColor. The disabled-font changes only when I change the BackColor attribute.

    I experimented with DrawStringDisabled, but I didn't get it to work. What I did was this:
    Whenever the apply-button is hit, I want it to become disabled, so the button-clicked-event received the following code:

    buttonApply.Enabled = false;
    Graphics g = Graphics.FromHwnd(buttonApply.Handle); // is this correct?
    ControlPaint.DrawStringDisabled(g, "Apply", buttonApply.Font, Color.Transparent,
    new Rectangle(0, 0, buttonApply.Width, buttonApply.Height), // is this correct?
                    StringFormat.GenericTypographic);
    // Followed by various stuff concering the applying of all changes.

    My button's behavior didn't change however. The disabled-font still looks the same.

  • Wednesday, September 12, 2012 5:07 PM
     
     Answered Has Code

    Always do Painting in the controls paint event.

    When the enabled state is changed the button will automagically repaint itself.

    Add the following method to your form and associate the buttons Paint event with it:

            private void btnApply_Paint(object sender, PaintEventArgs e)
            {
                Rectangle bounds = btnApply.ClientRectangle;
                bounds.Inflate(-3, -3);
    
                string text = this.btnApply.Text;
                Font font = this.btnApply.Font;
    
                using (SolidBrush backBrush = new SolidBrush(btnApply.BackColor))
                {
                    e.Graphics.FillRectangle(backBrush, bounds);
                }
    
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center; 
    
                    if (this.btnApply.Enabled)
                    {
                        using (Brush foreBrush = new SolidBrush(btnApply.ForeColor))
                        {
                            e.Graphics.DrawString(text, font, foreBrush, bounds, sf);
                        }
                    }
                    else
                    {
                        ControlPaint.DrawStringDisabled(e.Graphics, text, font, SystemColors.Control, bounds, sf);
                    }
                }
            }
    

    You now have the ability to paint the text in any color you like regardless of any backcolor or forecolor settings.


    Mick Doherty
    http://dotnetrix.co.uk
    http://glassui.codeplex.com

    • Marked As Answer by __Alexander__ Monday, September 17, 2012 9:56 AM
    •  
  • Monday, September 17, 2012 9:56 AM
     
     

    Awesome, that's exactly what I needed! Thank you!

    I only changed one thing, because I have a few more buttons that need this event. At the beginning I added
    "Button b = (Button) sender;" and then I replaced "this.btnApply" with it.

    And now I learned about the using-keyword as well. :-)