-1

I need to do a button on windows form in c# that maximize and make window normal size every time I push it. I try a code, but it works just if the window is already maximize and just a half of it. How can I solve this? My code is:

if (this.WindowState == FormWindowState.Maximized){
    this.WindowState = FormWindowState.Normal;
}
if (this.WindowState == FormWindowState.Normal){
    this.WindowState = FormWindowState.Maximized;
}

Aimnox
  • 895
  • 7
  • 20

2 Answers2

6

it's propably just a missing else. you set the window state to normal, then check if it's normal and maximise it again:

if (this.WindowState == FormWindowState.Maximized)
{
    this.WindowState = FormWindowState.Normal;
}
else if (this.WindowState == FormWindowState.Normal)
{
    this.WindowState = FormWindowState.Maximized;
}
d4zed
  • 478
  • 2
  • 6
  • 16
  • 2
    I suggest to use the [conditional operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator) – Steve Jan 15 '20 at 08:42
0

You are using If condition Twice At First the maximized window will become normal, and at next line if Condition will be true and again it will maximize the normal window . Either you can use If and Else If or go for Conditional Operator.

Conditional Operator:

this.WindowState = (this.WindowState == FormWindowState.Maximized)?FormWindowState.Normal:FormWindowState.Maximized;