Skip to content

IVSoftware/winforms-disable-button

Repository files navigation

Here's a lightweight extended class that uses ControlPaint.DrawButton to render the blank button so that you can cleanly draw the colored text over it.

disabled button demo

protected override void OnPaint(PaintEventArgs e)
{
    if (Enabled)
    {
        base.OnPaint(e);
    }
    else
    {
        var buttonState = Enabled ? ButtonState.Normal : ButtonState.Inactive;
        ControlPaint.DrawButton(e.Graphics, ClientRectangle, buttonState);

        Color textColorWithTransparency = 
            Color.FromArgb(
                0xA0,                   
                ForeColorDisabled);
                
        using (var brush = new SolidBrush(textColorWithTransparency))
        {
            e.Graphics.DrawString(
                Text,
                Font,
                brush,
                ClientRectangle,
                new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                });
        }
    }
}

You can set the disabled fore color in the designer:

designer


You will want to manually change instances of Button in the designer to instances of ButtonEx.

designer


Releases

No releases published

Packages

No packages published

Languages