简体   繁体   English

C# 复选框优化

[英]C# Checkboxes Optimisation

I have a panel with 5 checkboxes in it, one for each weekday.我有一个面板,里面有 5 个复选框,每个工作日一个。 The user in this scenario is booking appointments on the week.此场景中的用户正在预订一周的约会。 I have a WeeklyBooking class .我有一个WeeklyBooking 课程 This class has five bools, Monday, Tuesday, Wednesday, Thursday, Friday.这个类有五个布尔值,星期一、星期二、星期三、星期四、星期五。 By default, all bools are set to false.默认情况下,所有布尔值都设置为 false。 When a checkbox is checked, the corresponding bool becomes true.当复选框被选中时,相应的布尔值变为真。

        private void CheckCBs(WeeklyBooking week)
    {
        if (MondayCB.Checked)
        {
            week.monday = true;
        }
        if (TuesdayCB.Checked)
        {
            week.tuesday = true;
        }
        if (WednesdayCB.Checked)
        {
            week.wednesday = true;
        }
        if (ThursdayCB.Checked)
        {
            week.thursday = true;
        }
        if (FridayCB.Checked)
        {
            week.friday = true;
        }
        return;
    }

This is a method I call in a different method while I build the object to be put in an SQL Table.这是我在构建要放入 SQL 表的对象时在不同方法中调用的方法。 Does Anybody have any advice for how to optimise this method?有人对如何优化此方法有任何建议吗? - Thanks. - 谢谢。

The if() statements are not necessary. if()语句不是必需的。 The Checked property of ComboBox is already a bool and will be false if the checkbox is unchecked. ComboBoxChecked属性已经是一个bool ,如果未选中该复选框,则该属性将为 false。 Simply assign the value of the Checked property to your WeeklyBooking object properties:只需将Checked属性的值分配给您的WeeklyBooking对象属性:

private void CheckCBs(WeeklyBooking week)
{
    week.monday = MondayCb.Checked;
    week.tuesday = TuesdayCb.Checked;
    week.wednesday = WednesdayCb.Checked;
    week.thursday = ThursdayCb.Checked;
    week.friday= FridayCb.Checked;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM