C#一次更改所有按钮的背景颜色
C# Change all the buttons' background color at once
我有一个机场交通巴士的座位预订应用程序。我在文本框中输入乘客的姓名,然后单击其中一个按钮(座位),然后它的颜色从绿色变为红色,并显示以下消息:"The seat is reserved for 'name entered'." 如果我没有在文本框中输入名称,它会显示以下消息:"Please enter a name."
我想做一个复活节彩蛋。如果我输入一个特定的名字,它应该会保留所有的座位并立即将所有按钮的背景颜色更改为红色,但我没有编码。
提前致谢。
对于 WinForms,你可以使用这个:
using System.Linq;
private void SetButtonsBackColorToRed()
{
foreach ( var button in Controls.OfType<Button>() )
button.BackColor = Color.Red;
}
这里获取所有Button类型的控件,解析结果列表改变背景颜色。
备选
Controls.OfType<Button>().ToList().ForEach(button => button.BackColor = Color.Red);
我有一个机场交通巴士的座位预订应用程序。我在文本框中输入乘客的姓名,然后单击其中一个按钮(座位),然后它的颜色从绿色变为红色,并显示以下消息:"The seat is reserved for 'name entered'." 如果我没有在文本框中输入名称,它会显示以下消息:"Please enter a name." 我想做一个复活节彩蛋。如果我输入一个特定的名字,它应该会保留所有的座位并立即将所有按钮的背景颜色更改为红色,但我没有编码。 提前致谢。
对于 WinForms,你可以使用这个:
using System.Linq;
private void SetButtonsBackColorToRed()
{
foreach ( var button in Controls.OfType<Button>() )
button.BackColor = Color.Red;
}
这里获取所有Button类型的控件,解析结果列表改变背景颜色。
备选
Controls.OfType<Button>().ToList().ForEach(button => button.BackColor = Color.Red);