using System.Collections.Generic; using System.Windows.Forms; namespace Infrastructure { public class ControlStateMachine { private readonly Control owningControl; private readonly Dictionary controlChangesPerState = new Dictionary(); public ControlStateMachine(Control owner) { owningControl = owner; } public ControlChanges WhenStateChangesTo(T state) { var changesForState = new ControlChanges(owningControl); controlChangesPerState[state] = changesForState; return changesForState; } public void ChangeStateTo(T newState) { ControlChanges changesForState; if (!controlChangesPerState.TryGetValue(newState, out changesForState)) return; changesForState.Execute(); } } }