I have ten checkboxes and Enum.
I want to bind this checkboxes to Enum.
The problem is that there is a lot of consistency of the code.
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--;
}
}
}
Is it possible to simplify?
Use the
Control.Tagproperty to store the enum value for each checkbox, and use a commonCheckedChangedevent handler:The common event handler receives the
Checkboxassender, and it can extract the enum value fromTag: