Anton
Anton

Reputation: 940

Possible to have one checkBox CheckedChanged event for all checkBoxes?

I have a form that has ten checkBoxes. They all have the exact same code in them and by that I mean a single call to a method. I'm just curious is there a way to have a single generic CheckedChanged event?

private void checkBox_one_CheckedChanged(object sender, EventArgs e)
{
    enableImage();
}

private void checkBox_two_CheckedChanged(object sender, EventArgs e)
{
    enableImage();
}

I've got ten of those. Is there anyway to simplify this so I can just have a single event that captures them all?

Upvotes: 0

Views: 1595

Answers (2)

bokibeg
bokibeg

Reputation: 2142

Hold Shift, select the check boxes in question, press F4 and assign the event to all check boxes at once.

Upvotes: 1

Galma88
Galma88

Reputation: 2546

You only need to attach the same event to all of checkboxes:

        <CheckBox Name="_chkOne" Checked="CheckBox_Checked" IsChecked="{Binding 
                  Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" 
                  HorizontalAlignment="Center" 
                  VerticalAlignment="Center"/>

        <CheckBox Name="_chkTwo" Checked="CheckBox_Checked" IsChecked="{Binding 
                  Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" 
                  HorizontalAlignment="Center" 
                  VerticalAlignment="Center"/>

Upvotes: 5

Related Questions