using System.Collections.Generic; using System.Windows.Forms; namespace Infrastructure { public class ControlChanges { private readonly Control owningControl; private readonly List controlsToMakeVisible = new List(); private readonly List controlsToMakeInvisible = new List(); private readonly List onlyVisibleControls = new List(); private readonly List actions = new List(); public ControlChanges(Control host) { this.owningControl = host; } public ControlChanges MakeVisible(params Control[] controls) { this.controlsToMakeVisible.AddRange(controls); return this; } public ControlChanges MakeInvisible(params Control[] controls) { this.controlsToMakeInvisible.AddRange(controls); return this; } public ControlChanges TheOnlyVisibleControlsAre(params Control[] controls) { this.onlyVisibleControls.AddRange(controls); return this; } public ControlChanges Do(Action action) { this.actions.Add(action); return this; } public void Execute() { this.owningControl.SuspendLayout(); this.ExecuteMakeInvisible(); this.ExecuteMakeVisible(); this.ExecuteTheOnlyVisibleControlsAre(); this.ExecuteActions(); this.owningControl.ResumeLayout(); } private void ExecuteMakeInvisible() { foreach (Control control in this.controlsToMakeInvisible) control.Visible = false; } private void ExecuteMakeVisible() { foreach (Control control in this.controlsToMakeVisible) control.Visible = true; } private void ExecuteTheOnlyVisibleControlsAre() { if (onlyVisibleControls.Count == 0) return; foreach (Control control in this.owningControl.Controls) { control.Visible = this.ControlIsVisible(control, this.onlyVisibleControls); } } private bool ControlIsVisible(Control control, ICollection visibleControls) { return visibleControls.Contains(control); } private void ExecuteActions() { foreach (Action action in actions) action(); } } }