We help IT Professionals succeed at work.

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

roujesky
roujesky asked
on
1,882 Views
Last Modified: 2016-10-17
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!
Comment
Watch Question

Russ SuterSenior Software Developer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
How do I do it only when it is clicked?
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Commented:
That's what overriding the OnClick method is for and the private member _isClicked.
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
that did it!

thanks!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.