【问题标题】:How to delete button border after clicking outside of Form C#?在 C# 外部单击后如何删除按钮边框?
【发布时间】:2019-05-16 00:25:27
【问题描述】:

我制作了一个简单的按钮,但是当我在 win 表单之外单击时,我的按钮会出现黑色边框。顺便说一句,我将BorderSize 设置为“0”,当我在表单内部单击时效果很好。

this.button.FlatAppearance.BorderSize = 0;

就是这个样子。

【问题讨论】:

  • 看看this answer 看看它是否能解决你的问题
  • 这个answer 为我解决了。

标签: c# .net winforms winapp


【解决方案1】:

一个简单的解决方法是将按钮的FlatAppearance.BorderColor 设置为其Parent.BackColor

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

如果可以在某个时候将控件分配给另一个父级,则可以将此属性设置为订阅ParentChanged 事件(或覆盖OnParentChanged,如果它是自定义控件)。

您也可以批量执行相同的操作,使用HandleCreated 事件并让所有按钮(带有FlatStyle = FlatStyle.Flat)在表单的构造函数中订阅该事件:

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

【讨论】:

    【解决方案2】:

    将这些代码行添加到表单加载事件中。

     btn.FlatStyle = FlatStyle.Flat;//You can also use the popup flat style
     btn.FlatAppearance.BorderColor = btn.Parent.BackColor;
     btn.FlatAppearance.BorderSize = 0;
    

    【讨论】:

      【解决方案3】:

      这似乎是一个焦点问题。尝试在光标离开控件时重置焦点。

      【讨论】:

        猜你喜欢
        • 2016-06-27
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多