将枚举映射到用户选择复选框

问题描述 投票:2回答:1

目前我有以下几点

if ((int)dpRepeatType.SelectedValue == (int)Constants.RepeatType.Weekly)
{
                 wrule = new WeeklyRecurrenceRule(Convert.ToDateTime(dtDateStart.Value),WeekDays.Monday, 1);
                _newAppointment.RecurrenceRule = wrule.ToString();

}

在屏幕上,我有7个复选框,代表一周的日期。周日到周六我的问题是WeekDay是telerik rad调度程序的内部枚举,基于以下内容。

我的问题不是在单个复选框上做if语句的语气,看看用户选择哪一天或多天可以多于一个,我怎么能用linq做这个目前我用if语句做但我是确定有更好的方法。

[Flags]
    public enum WeekDays
    {
        //
        // Summary:
        //     Specifies none of the days
        None = 0,
        //
        // Summary:
        //     Specifies the first day of the week
        Sunday = 1,
        //
        // Summary:
        //     Specifies the second day of the week
        Monday = 2,
        //
        // Summary:
        //     Specifies the third day of the week
        Tuesday = 4,
        //
        // Summary:
        //     Specifies the fourth day of the week
        Wednesday = 8,
        //
        // Summary:
        //     Specifies the fifth of the week
        Thursday = 16,
        //
        // Summary:
        //     Specifies the sixth of the week
        Friday = 32,
        //
        // Summary:
        //     Specifies the work days of the week
        WorkDays = 62,
        //
        // Summary:
        //     Specifies the seventh of the week
        Saturday = 64,
        //
        // Summary:
        //     Specifies the weekend days of the week
        WeekendDays = 65,
        //
        // Summary:
        //     Specifies every day of the week
        EveryDay = 127
    }
}

这就是我想要达到的目标。

enter image description here

c# .net linq checkbox enums
1个回答
3
投票

让我们说你的形式中唯一的CheckBox控件是关于te周日和他们的Name属性遵循以下模式:

CheckBoxMonday
CheckBoxTuesday
...

一种解决方案可能是以下一种:

WeekDays wd = WeekDays.None;

foreach (CheckBox checkBox in this.Controls.OfType<CheckBox>())
{   
    if (checkBox.IsChecked)
        wd |= (WeekDays)Enum.Parse(typeof(WeekDays), checkBox.Name.Replace("CheckBox", ""));

}

演示代码here

© www.soinside.com 2019 - 2024. All rights reserved.