Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I change the border color of a button?

Tags:

c#

winforms

This is my code:

buttonName = "btn" + y.ToString() + x.ToString();
Control btn = this.Controls.Find(buttonName, true)[0] as Control;
btn.BackColor = System.Drawing.Color.Blue;

However, I see no border color changing properties, or the like.

I used this code because I have a lot of buttons on my form, and any of those buttons' properties can change, so rather than call them out individually, I just made up that code which could handle them.

Is there a code similar to the one above, that would allow me to change the border color of the button?

like image 579
zack_falcon Avatar asked Nov 23 '11 15:11

zack_falcon


People also ask

How do I change the color of my outline button?

To change the Outlined Button color in Flutter, simply set the style property of Outlined Button from the OutlinedButton. styleFrom() static method and set the backgroundColor property to change background color and primary property to change the text color.


4 Answers

I am not sure what sort of application you are working on, however in winforms there is no border property for a button directly on it, even in the designer. You can use a flat style button. And your type will have to be button.

you can do it like:

buttonName = "btn" + y.ToString() + x.ToString();
Button btn = this.Controls.Find(buttonName, true)[0] as Button;
btn.BackColor = System.Drawing.Color.Blue;
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderColor = Color.Red;
btn.FlatAppearance.BorderSize = 1;

Unfortunately, this will only work on button with a FlatStyle.

like image 76
Jay Avatar answered Oct 19 '22 07:10

Jay


you can use the flatAppearance.BorderColor

    btn.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
like image 38
BDubCook Avatar answered Oct 19 '22 06:10

BDubCook


I know that this question is asked for a long time ago ( at 2011 ), but I think my comment will useful for someone : without using FlatStyle, you can use ControlPaint.DrawBorder in Paint event of the button you want to change the border color

    private void btnName_Paint(object sender, PaintEventArgs e)
    {
        Button btn = (Button)sender;

        ControlPaint.DrawBorder(e.Graphics, btn.ClientRectangle,
                                Color.LightGreen, 1, ButtonBorderStyle.Solid,
                                Color.LightGreen, 1, ButtonBorderStyle.Solid,
                                Color.LightGreen, 1, ButtonBorderStyle.Solid,
                                Color.LightGreen, 1, ButtonBorderStyle.Solid
                                );
    }

According to Microsoft (link)

Applies to

.NET Framework

4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

like image 5
VietDD Avatar answered Oct 19 '22 06:10

VietDD


Depending on your framework there is a new property called BorderColor

http://msdn.microsoft.com/en-us/library/system.windows.forms.flatbuttonappearance.bordercolor.aspx,

Have you checked that?

Also here is an example of something similar

Change border color of Windows Forms Control on focus

like image 3
user182630 Avatar answered Oct 19 '22 06:10

user182630