Home > Software design >  OnEnabledChanged can't set Backcolor Disable for button
OnEnabledChanged can't set Backcolor Disable for button

Time:02-24

This is code sample

 public partial class ColorButton: Button
{
     protected override void OnEnabledChanged(EventArgs e)
            {
                if (this.Enabled)
                {
                    this.BackColor = this.BackColorEnable;              
                }
                else
                {
                    this.BackColor = Color.Gray;
                }
                base.OnEnabledChanged(e);
                
            }

    pubic void ColorSet()
    {
            if (BaseColor == Blue)
                {
                   this.BackColor = Color.Blue;
                }
            if (BaseColor == Red)
                {
                   this.BackColor = Color.Red;
                }
            if (BaseColor == Yellow)
                {
                   this.BackColor = Color.Yellow;
                }
    }
}

Buttons will be automatically colored on another screen and btn.enable = flase; also adjusted red, yellow, green. i want the button 'btn.enable = flase;' Must be Gray. Does anyone have any ideas?

CodePudding user response:

Change code in here

public void ColorSet()
{
    if (BaseColor == Blue)
    {
        if(this.Enable)
        {
            this.BackColor = Color.Blue;
        }
        else
        {
            this.BackColor = Color.Gray;
        }
    }
    if (BaseColor == Red)
    {
        if(this.Enable)
        {
            this.BackColor = Color.Red;
        }
        else
        {
            this.BackColor = Color.Gray;
        }
    }
    if (BaseColor == Yellow)
    {
        if(this.Enable)
        {
            this.BackColor = Color.Yellow;
        }
        else
        {
            this.BackColor = Color.Gray;
        }
    }
}

I think making the OnEnabledChanged function does not really do its job, so just put the if(this.Enable) condition in.

  • Related