SetC#:以编程方式设置FlatStyle

时间:2018-01-31 21:07:18

标签: c# programmatically

我在C#中创建了一个类,我想用它来设置调用它的每个表单上的某些控件的属性。像样式表的替代品。它适用于某些属性,但不适用于其他属性。例如,FlatStyle会出现错误" Control不包含' FlatStyle' ...

的定义
public static void SetAttributesTEST(Form thisForm)
{
foreach (Control C in CallingForm.Controls)
            {
                if (C is TextBox)
                {
                    C.ForeColor = Color.SlateGray;
                    C.BackColor = Color.White;
                    C.Font = new Font("Segoe UI", 12, FontStyle.Regular);
                }
                if (C is Label)
                {
                    C.ForeColor = Color.SteelBlue;
                    C.BackColor = Color.White;
                    C.Font = new Font("Segoe UI", 12, FontStyle.Regular);
                }
                if (C is Button)
                {
                    C.ForeColor = Color.White;
                    C.BackColor = Color.SteelBlue;
                    C.Font = new Font("Segoe UI", 12, FontStyle.Regular);
                    C.FlatStyle = FlatStyle.Flat;
                }
}
}

1 个答案:

答案 0 :(得分:1)

C#7糖:Link

        foreach (Control control in Controls)
        {
            control.Font = new Font("Segoe UI", 12, FontStyle.Regular);

            switch (control)
            {
                case TextBox tbx:
                    tbx.ForeColor = Color.SlateGray;
                    tbx.BackColor = Color.White;
                    break;

                case Label lbl:
                    lbl.ForeColor = Color.SteelBlue;
                    lbl.BackColor = Color.White;
                    break;

                case Button btn:
                    btn.ForeColor = Color.White;
                    btn.BackColor = Color.SteelBlue;
                    btn.FlatStyle = FlatStyle.Flat;
                    break;
            }
        }