CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    How to change the label disabled forecolor

    As the title suggests,
    how to change the standard (default) forecolor when the label is disabled.

    I came up with the following:
    Code:
    namespace COD
    {
        class CustomLabel : System.Windows.Forms.Label
        {
            private bool _enabled = true;
    
            protected override bool Enabled
            {
                get
                {
                    return _enabled;
                }
            
                set
                {
                    _enabled = value;
    
                    if (_enabled)
                        this.ForeColor = Color.FromKnownColor(KnownColor.Yellow);
                    else
                        this.ForeColor = Color.FromKnownColor(KnownColor.Red);
                }
            }
        }
    }
    but it is not working ....

    regards,

    G.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to change the label disabled forecolor

    That won't work because it's the implementation of the OnPaint() method somewhere up the inheritance chain that decides the disabled color, and all controls have similar appearance when disabled for consistency.

    You could override the OnPaint() method and do something like this:
    Code:
    public class CustomLabel : Label
    {
        public CustomLabel()
        {            
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!Enabled)
            {
                using (Label temp = new Label())
                using (Bitmap bitmap = new Bitmap(Width, Height))
                {                    
                    // Copy the relevant properties
                    temp.BackColor = BackColor;
                    temp.AutoSize = AutoSize;
                    temp.Size = Size;
                    temp.Text = Text;
    
                    temp.ForeColor = Color.Red;   // the new disabled color          
    
                    temp.DrawToBitmap(bitmap, temp.ClientRectangle);   // (1) draw the temporary label to a bitmap, and then
    
                    e.Graphics.DrawImage(bitmap, Point.Empty);    // (2) draw that bitmap instead of the label object
                }
            }
            else
            {
                base.OnPaint(e);
            }
        }
    }

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Cool Re: How to change the label disabled forecolor

    A quicker approach would be to use this.SetStyle(ControlStyles.UserPaint, true); as in the following example :

    CustomLabel.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace COD
    {
        public class CustomLabel : Label
        {
            public CustomLabel()
            {
                this.SetStyle(ControlStyles.UserPaint, true); //Call in constructor, Use UserPaint
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                if (!Enabled)
                {
                    SolidBrush drawBrush = new SolidBrush(Color.DarkSlateBlue); //Choose colour
                   
                    e.Graphics.DrawString(Text, Font, drawBrush, 0f, 0f); //Dra whatever text was on the label
                }
                else
                {
                    base.OnPaint(e); //Default Forecolours
                }
            }
        }
    }

    YOURFORM.css :

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                customLabel1.Enabled = false; //Disabled
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                customLabel1.Enabled = true; //Enabled
            }
    Easy!

    Have a look at the attachment.

    Hannes
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; November 12th, 2012 at 09:30 AM. Reason: added attachment

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to change the label disabled forecolor

    Check out moving to WPF where you have increased control over customizing all the visual elements.

  5. #5
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to change the label disabled forecolor - Counterexample

    @HanneSThEGreaT:
    That approach is a bit quicker, and I've considered it, but there's a slight problem: text drawn from the string may not render exactly the same way as the corresponding text on the label - there could be a slight offset in placement and maybe even font character width (even if it's the same font), depending on the exact way the Graphics class renders it.

    I attached a counterexample project which demonstrates both techniques on two font sizes (I just added a large font version where that the effect is more visible).
    Attached Files Attached Files
    Last edited by TheGreatCthulhu; November 12th, 2012 at 06:26 PM.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to change the label disabled forecolor - Counterexample

    Quote Originally Posted by TheGreatCthulhu View Post
    @HanneSThEGreaT:
    That approach is a bit quicker, and I've considered it, but there's a slight problem: text drawn from the string may not render exactly the same way as the corresponding text on the label - there could be a slight offset in placement and maybe even font character width (even if it's the same font), depending on the exact way the Graphics class renders it.

    I attached a counterexample project which demonstrates both techniques on two font sizes (I just added a large font version where that the effect is more visible).
    Interesting. Just proves you can teach an old dog new tricks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured