将位标志绑定到复选框

时间:2012-06-13 17:52:50

标签: c# winforms enums checkbox

我有10个复选框和Enum。 我想将此复选框绑定到Enum。 问题是代码有很多一致性。

public partial class RightsSetForm : Form
    {
        public RightsEnum rights;
        public int count = 1;
        public RightsSetForm()
        {
            InitializeComponent();
            rights |= RightsEnum.notify;
        }

        private void chkNotify_CheckedChanged(object sender, EventArgs e)
        {

            if (chkNotify.Checked)
            {
                rights |= RightsEnum.notify;
                count++;
            }
            else
            {
                rights ^= RightsEnum.notify;
                count--;
            }
        }

        private void chkFriends_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFriends.Checked)
            {
                count++;
                rights |= RightsEnum.friends;
            }
            else
            {
                rights ^= RightsEnum.friends;
                count--;
            }
        }
}

是否可以简化?

3 个答案:

答案 0 :(得分:2)

您可以为每个复选框分配一个事件处理程序,将每个复选框强制转换为CheckBox,然后在Name属性(CheckBox的标签)上切换语句以选择枚举。类似的东西:

private void checkbox_CheckedChanged(object sender, EventArgs e)
{
    var checkBox = (CheckBox)sender;

    RightsEnum rightsEnum = RightsEnum.notify;
    switch (checkBox.Name)
    {
        case "chkNotify":
            rightsEnum = RightsEnum.notify;
            break;
        case "chkFriends":
            rightsEnum = RightsEnum.friends;
            break;    
        // to do:  add the remaining 8       
    }

    if (checkBox.Checked)
    {
         count++;
         rights |= rightsEnum;
    }
    else
    {
         rights ^= rightsEnum;
         count--;
    }
}

答案 1 :(得分:1)

使用Control.Tag属性存储每个复选框的枚举值,并使用常见的CheckedChanged事件处理程序:

chkFriends.Tag = RightsEnum.friends;
chkNotify.Tag = RightsEnum.notify;

chkFriends.CheckedChanged += enumCheckedChanged;
chkNotify.CheckedChanged += enumCheckedChanged;

公共事件处理程序将Checkbox作为sender接收,它可以从Tag中提取枚举值:

void enumCheckedChanged(object sender, EventArgs e)
{
    var checkbox = (Checkbox)sender;

    var flag = (RightsEnum)checkbox.Tag;

    if (checkbox.Checked)
    {
        count++;
        rights |= flag;
    }
    else
    {
        rights ^= flag;
        count--;
    }
}

答案 2 :(得分:1)

Inmx有一个良好的开端,但他确实错过了一些事情。

private void enumCheckedChanged(Checkbox pBox, bool pbAddFirst) {
    if (!(pBox.Tag is RightsEnum)) return;

    RightsEnum flag = pBox.Tag as RightsEnum;

    if (pBox.Checked && pbAddFirst) {
         count++;
         rights |= flag;
    }
    else if (pBox.Checked && !pbAddFirst) {
         rights |= flag;
         count++;
    }
    // Having checked for both variations of pbAddFirst, the only thing left would be if pBox.Checked is false
    else {
         rights ^= flag;
         count--;
    }
}

如果您碰巧需要先减去计数,您可以随时再次使用pbAddFirst修改else。

然后在每个CheckBox的Check事件中,您只需要调用方法:

private void chkFriends_CheckedChanged(object sender, EventArgs e){
    enumCheckedChanged(chkFriends, true);
}

使用Checkbox的Tag属性绝对是一个很好的调用。如果将数据对象绑定到UI对象,这将使您的生活变得更加轻松。