Is there any way to merge these voids into one? I have a lot of controls that use the same event and during execution are activated and deactivated
private void activate()
{
tb1.TextChanged = eventFil;
tb2.TextChanged = eventFil;
cb1.CheckStateChanged = eventFil;
cb2.CheckStateChanged = eventFil;
}
private void deactivate()
{
tb1.TextChanged -= eventFil;
tb2.TextChanged -= eventFil;
cb1.CheckStateChanged -= eventFil;
cb2.CheckStateChanged -= eventFil;
}
Like this
private void activate(operator aux)
{
tb1.TextChanged aux eventFil;
tb2.TextChanged aux eventFil;
cb1.CheckStateChanged aux eventFil;
cb2.CheckStateChanged aux eventFil;
}
Or like this
private void activate()
{
aux(c => c = eventFiltre);
}
private void deactivate()
{
aux(c => c -= eventFiltre);
}
private void aux(Action<EventHandler> aux)
{
aux(tb1.TextChanged);
....
}
CodePudding user response:
I think it's not possible do that because = and -= only can appear on an assignement. You can have all in one method for better maintenance, but with two sentences for each event:
private void EnableEvents(bool enabled = true)
{
if (enabled)
{
tb1.TextChanged = eventFil;
tb2.TextChanged = eventFil;
cb1.CheckStateChanged = eventFil;
cb2.CheckStateChanged = eventFil;
}
else
{
tb1.TextChanged -= eventFil;
tb2.TextChanged -= eventFil;
cb1.CheckStateChanged -= eventFil;
cb2.CheckStateChanged -= eventFil;
}
}
CodePudding user response:
You can use one method like this
//you can pass for value activate and deactivate
private void Operate(string value)
{
if(value = "activate")
{
tb1.TextChanged = eventFil;
tb2.TextChanged = eventFil;
cb1.CheckStateChanged = eventFil;
cb2.CheckStateChanged = eventFil;
}
else
{
tb1.TextChanged -= eventFil;
tb2.TextChanged -= eventFil;
cb1.CheckStateChanged -= eventFil;
cb2.CheckStateChanged -= eventFil;
}
}