How to delete button border after clicking outside of Form C#?

放肆的年华 提交于 2019-12-13 06:33:25

问题


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.


回答1:


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




回答2:


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;



回答3:


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; 
        };
    }
}


来源:https://stackoverflow.com/questions/53784690/how-to-delete-button-border-after-clicking-outside-of-form-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!