Calling the same function for multiple objects in C# -
how can use function in c# accepts object parameter?
e.g., have many checkboxes in form of runs same procedure, not want write same function call inside each of checkbox events:
private void checkbox1_checkedchanged(object sender, eventargs e) { if (checkbox1.checked) { callmyfunction(); } }
what want create event handler listens of checkbox objects registered event. whenever checkbox_checkedchanged event triggered, function runs.
if checkbox 1 raising event, sender
should checkbox. can check type of sender
, use instead of explicitly naming checkbox:
private void checkbox_checkedchanged(object sender, eventargs e) { checkbox cb = sender checkbox; if(cb != null , cb.checked) { callmyfunction(cb); } }
you can add same handler checkboxes e.g.
checkbox1.checkedchanged += checkbox_checkedchanged; checkbox2.checkedchanged += checkbox_checkedchanged; ...
Comments
Post a Comment