如何更改WinForms面板中几个按钮的FlatStyle MouseDown BackColor



我需要解析面板中的控件来更新一些按钮。我不明白如何访问这个来更改按钮扁平式鼠标向下颜色

public Color MouseDownBackColor {get; set;}

我知道我可以使用this.button1.FlatAppearance.MouseDownBackColor =,但在这种情况下,我将从面板访问的按钮作为var,但它不能请通过这种方式访问它。

更新:

foreach (Control control in button_panel.Controls)
{
if (control is Button)
{
var button = (Button)control;
button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 0, 0, 0);
}
}

按钮的FlatStyle属性必须设置为FlatStyle.Flat才能工作。

在类型检查之后,可以对Button使用类型强制转换(开箱(:

foreach (Control control in button_panel.Controls)
{
if (control is Button)
{
var button = (Button)control;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.MouseDownBackColor = Color.Yellow;
}
}

你也可以用Linq:写这个

using System.Linq;
button_panel.Controls.OfType<Button>().ToList().ForEach(button =>
{
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.MouseDownBackColor = Color.Yellow;
});

最新更新