Link to home
Start Free TrialLog in
Avatar of roujesky
roujesky

asked on

C# Winform Button, make the border thicker and different color?

I have a Button that when clicked I would like to change the color of the button as well as the thickness of the border.   Is that possible?

thanks!
Avatar of Russ Suter
Russ Suter

It is possible if you subclass the control and handle the paint event manually. Here is the crudest of examples to get you started.
    public class MyButton : Button
    {
        private bool _isClicked = false;

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (_isClicked)
            {
                pevent.Graphics.FillRectangle(Brushes.Red, this.ClientRectangle);
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            this._isClicked = true;
        }
    }

Open in new window

You would still need to draw the border and text to make it look right.
Avatar of roujesky

ASKER

How do I do it only when it is clicked?
That's what overriding the OnClick method is for and the private member _isClicked.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that did it!

thanks!