建立枚举标志列表

时间:2019-12-06 19:37:38

标签: c# winforms enum-flags

我有这种方法可以接受多个JobSetting标志

private void ProcessTheData(string[] dataFiles, PictureBox sender, JobSetting? jobSetting)

Flags枚举是这个

[Flags]
enum JobSetting
{
    ForcePrint = 1,
    ForceProof = 2,
    UseDefault = 4,
    SkipImposition = 8,
    HighPriority = 16,
    SetDP = 32
}

我有一个事件处理程序,它查看CheckboxList,并根据选中的选项构建标志列表。

JobSetting js;
if (((Control)sender).Name.ToLower().Contains("proof"))
{
    if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority") && chkbxList_Proof.CheckedItems.Contains("_SetDP"))
    {
        js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority | JobSetting.SetDP);
    }
    else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority"))
    {
        js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority);
    }
    else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
    {
        js = (JobSetting.ForceProof | JobSetting.SkipImposition);
    }
    else if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
    {
        js = (JobSetting.ForceProof | JobSetting.HighPriority);
    }
    else if (chkbxList_Proof.CheckedItems.Contains("_SetDP"))
    {
        js = (JobSetting.ForceProof | JobSetting.SetDP);
    }
    else
    {
        js = JobSetting.ForceProof;
    }
}

所有方法都可以...但是我觉得那是建立我的标志列表的最不有效的方法。

我想以这种方式或任何正确的方式来构建它。

JobSeetings js = JobSetting.ForceProof;
if(chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
    js.add(JobSetting.SkipImposition)
}
if(chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
    js.add(JobSetting.HighPriority)
}

1 个答案:

答案 0 :(得分:1)

您可以根据所检查的项目将标志与按位或进行组合。

JobSetting js = JobSetting.ForceProof;
if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
   js |= JobSetting.SkipImposition;
}
if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
   js |= JobSetting.HighPriority;
}
//etc, etc...

如果选中"Skip Imposition""High Priority",则js将等于JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority