So I thought that an enum would help me with States. Basically, I have a little Sidebar app which has 3 states:
Minimized – Where you can see a small and colored rectangular Panel indicating the app is open on the desktop.
Preview – Which indicated the app is open, and you can see the logo.
Normal – where the whole sidebar can be seen.
Now, I have setup an enum like this:
public enum CurrentState
{
Minimized = 0,
Preview,
Normal
};
And I am trying to check the CurrentState and switch to another like this when the user clicks the always-visible panel:
// If Min=Set(Preview). If Preview=Set(Normal). If Normal=Set(Min).
if (State.HasFlag(CurrentState.Minimized))
{
State = CurrentState.Preview;
this.Location = new Point(
Screen.PrimaryScreen.WorkingArea.Right - _minimize.Size.Width - _logo.Size.Width,
this.Location.Y
);
}
else if (State.HasFlag(CurrentState.Preview))
{
State = CurrentState.Normal;
this.Location = new Point(
Screen.PrimaryScreen.WorkingArea.Right - this.Size.Width,
this.Location.Y
);
}
else
{
State = CurrentState.Minimized;
this.Location = new Point(
Screen.PrimaryScreen.WorkingArea.Right - _minimize.Size.Width,
this.Location.Y
);
}
And when the application loads, I set the initial value to:
CurrentState.Minimized;, like this:
CurrentState State = CurrentState.Preview;
So, this is the expected behavior:
If form is Minimized, Move it a bit so it is in Preview mode. If it is in Preview, move it a bit so it is in Normal mode. If it is in Normal mode, set it to Minimized again.
But, upon clicking the always-visible panel the first time, it works as expected. It sets it to Preview mode and moved the form a bit.
But, that’s as far as it goes. Once it’s in Preview mode, it does not go to Normal mode upon clicking the Panel a second time, which means that it’s currently impossible to get it to normal view.
Am I doing something wrong here?
Although this may be uncommon, it is logical, and I can’t figure out where something is going wrong.
Here’s a little visual to help better clarify what I mean:

The code as you have it always enters the Minimized branch of the if/else structure.
The code below that doesn’t use the Flags but instead compares the enums works correctly and progresses through the states as you want:
One thought though, which would probably make this design a lot easier to work with is to implement the State pattern.
As the name suggests, this design pattern deals with exactly the situation you are trying to solve.
Essentially the logic for your three states would get split out into three seperate objects that implement something like
IWindowStatewhere IWindow state includes a method calledMoveState. WhenMoveStateis called, the state object for a Minimized window knows the next state is Preview, and the Preview object knows to go to Normal.For your case this might be design overkill, but as soon as you want any more complex logic like “move from minimized to preview unless the user has a setting to move directly to normal” then the pattern pays for itself in properly managing the complexity.