在Form C#外部单击后如何删除按钮边框? [英] How to delete button border after clicking outside of Form C#?
问题描述
我做了一个简单的按钮,但是当我在获胜表格之外单击时,我的按钮显示黑色边框.顺便说一句,我将BorderSize
设置为"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;
那是它的样子.
推荐答案
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;
};
}
}
这篇关于在Form C#外部单击后如何删除按钮边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!