Here is a situation we are all familiar with: A form that only displays a certain set of controls depending on the mode or state of the application. Let me start with an example: At design time there are three buttons

The user can look at the data and decide to edit it:

Or the user is editing the data and can decide to commit or discard her changes:

A couple of years i ago i used to spread such display logic all over my code and it was hard to figure out which control was visible at a given point. Later on i refactored that code and encapsulated it in functions like: MakeControlsForDisplayVisible and MakeControlsForEditVisible which felt like a huge improvement. These days i have the feeling that a very simple state machine can improve the readability even better.

Ok, so how simple is simple? Currently the requirements list is pretty limited:

Anyway, here is how i would write the code today (Yeah, for a stupid example this looks like overkill):

private void InitializeButtonLayoutPanelMachine()
{
controlStateMachine = new ControlStateMachine<displayAndEditStates>(buttonLayoutPanel);

controlStateMachine.WhenStateChangesTo(DisplayAndEditStates.Display)
.TheOnlyVisibleControlsAre(buttonEdit);

controlStateMachine.WhenStateChangesTo(DisplayAndEditStates.Edit)
.TheOnlyVisibleControlsAre(buttonSave, buttonCancel);
}


As always, here is the source: ControlStateMachine and WhenChangingState.