Reputation: 331
In my case i have 8 checkbox, each checkbox has click event, if checkbox is clicked, that will call same method like this.
public void _setting(CheckBox check)
{
...
}
And this is the code how click event call the method
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(check1);
}
How to simplify the click event to call method? Or i have to call like in check1_click until check8?
private void check2_Click(object sender, RoutedEventArgs e)
{
_setting(check2);
}
private void check3_Click(object sender, RoutedEventArgs e)
{
_setting(check3);
}
...
private void check8_Click(object sender, RoutedEventArgs e)
{
_setting(check8);
}
or can it be simplified?
Upvotes: 0
Views: 72
Reputation: 3073
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(sender as CheckBox);
}
This way if you call _setting and sender is not a checkbox, it will be null, not Exception.
Upvotes: 0
Reputation: 157116
I think so. It seems that check1
..check8
are the controls being clicked. In that case, you could assign all events to a single event handler and use the sender
:
private void check_Click(object sender, RoutedEventArgs e)
{
_setting((CheckBox)sender);
}
If sender is not the control you expect, possibly Source
or e.OriginalSource
is.
Upvotes: 3