这是我的代码
public static class ButtonProperties
{
public static void _ButtonProperties(Button btn_dashboard, Button btn_products, Button btn_supplier, Button btn_customer, Button btn_sales, Button btn_inventory, Button btn_settings)
{
btn_dashboard.FlatStyle = FlatStyle.Flat;
btn_dashboard.FlatAppearance.BorderSize = 0;
btn_products.FlatStyle = FlatStyle.Flat;
btn_products.FlatAppearance.BorderSize = 0;
btn_supplier.FlatStyle = FlatStyle.Flat;
btn_supplier.FlatAppearance.BorderSize = 0;
btn_customer.FlatStyle = FlatStyle.Flat;
btn_customer.FlatAppearance.BorderSize = 0;
btn_sales.FlatStyle = FlatStyle.Flat;
btn_sales.FlatAppearance.BorderSize = 0;
btn_inventory.FlatStyle = FlatStyle.Flat;
btn_inventory.FlatAppearance.BorderSize = 0;
btn_settings.FlatStyle = FlatStyle.Flat;
btn_settings.FlatAppearance.BorderSize = 0;
}
}
如您所见,这需要太多参数,我想使其更短。
答案 0 :(得分:11)
您可以使用params
-array来做到这一点:
public static class ButtonProperties
{
public static void _ButtonProperties(params Button[] buttons)
{
foreach (Button b in buttons)
{
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
}
}
}
现在您可以传递一个按钮,多个(逗号分隔)甚至是一个Button[]
。
例如:
ButtonProperties._ButtonProperties(btn_dashboard, btn_products);
答案 1 :(得分:4)
可以有一个列表,而不是固定的参数早午餐。
public static void SetButtonProperties(List<Button> buttons){
foreach(var button in buttons){
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
}
}
像这样调用它:
SetButtonProperties(new List<Button>{ btn_dashboard, btn_products, btn_supplier, ... })
答案 2 :(得分:3)
看到每个按钮都在做同样的事情,为什么不只传递一个列表呢?
public static void _ButtonProperties(List<Button> buttons)
{
foreach (var button in buttons)
{
button.FlatAppearance.BorderSize = 0;
button.FlatStyle = FlatStyle.Flat;
}
}