0

I made a simple button, but when i click outside of win form my button getting a black border. By the way i set BorderSize to "0" and it works great while i clicking inside of my form.

this.button.FlatAppearance.BorderSize = 0;

That's how it looks like.

example

Ryan Cowl
  • 53
  • 1
  • 4
  • Take a look at [this answer](https://stackoverflow.com/questions/148729/how-to-set-change-remove-focus-style-on-a-button-in-c) and check if it could solve your problem – Steve Dec 14 '18 at 18:13
  • This [answer](https://stackoverflow.com/a/9400099/5734097) solved for me. – D.Kastier Oct 08 '19 at 14:16

3 Answers3

0

One simple workaround is to set the FlatAppearance.BorderColor of the Button to its Parent.BackColor:

this.button1.FlatAppearance.BorderColor = this.button1.Parent.BackColor;

You could set this Property subscribing to the ParentChanged event (or overriding OnParentChanged, if it's a Custom Control) if the Control can be assigned to another parent at some point.

You can also perform the same operation in batch, using the HandleCreated event and have all the Buttons (with FlatStyle = FlatStyle.Flat) subscribe to the event in the Form's constructor:

public Form1()
{
    InitializeComponent();
    foreach (Button button in this.Controls.OfType<Button>().Where(btn => btn.FlatStyle == FlatStyle.Flat))
    {
        button.HandleCreated += (s, e) => { 
            button.FlatAppearance.BorderColor = button.Parent.BackColor; 
        };
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
0

it seems a focus problem. Try to reset the focus when the cursor leave the control.

0

Add these lines of code to your forms load event.

 btn.FlatStyle = FlatStyle.Flat;//You can also use the popup flat style
 btn.FlatAppearance.BorderColor = btn.Parent.BackColor;
 btn.FlatAppearance.BorderSize = 0;
preciousbetine
  • 2,959
  • 3
  • 13
  • 29