C# Custom Close, Minimize and Maximixe buttons

11,092

You are creating a new local restore variable in your set_Restore() method:

bool restore = true;

Try changing it to just:

restore = true;

I don't even think the variable is needed though. I think you can just do this:

private void MaximizeButton_Click(object sender, EventArgs e) {
  if (this.WindowState == FormWindowState.Maximized) {
    this.WindowState = FormWindowState.Normal;
  } else {
    this.WindowState = FormWindowState.Maximized;
  }
}
Share:
11,092
Niklas Jakobsen
Author by

Niklas Jakobsen

Updated on June 18, 2022

Comments

  • Niklas Jakobsen
    Niklas Jakobsen about 2 years

    I've been trying to create my own program, with custom close maximize and minimize buttons (like in Visual Studio, or Word 2013 etc...(my border style is set to "None")) So what I've been trying to do is creating three buttons. One with the closing option, (works fine) one with the minimize option, (also works fine) and one with the maximize button. alone the maximize button works fine, but i want to have it like standard windows buttons, so that when the form is maximized, it will restore the forms previous state (Normal) which i know can be done with

    this.WindowState = FormWindowState.Normal;
    

    But it should be with one button if you understand what i mean. What i've tried is making a bool, which's value is set to true when the form is maximized (with an "if" statement) and set to false when the form isn't maximized (else function). Now when the maximize button is clicked the form will maximize and therefore the boolean will be set to true, but when i click again, nothing happens! The other functions like close and minimize works just fine, i even made a "Restore" button, which works just fine!

    Any help appreciated, this is my code:

        bool restore;
    
        private void set_Restore()
        {
            {
                if (this.WindowState == FormWindowState.Maximized) //Here the "is" functions is
                {
                    restore = true; //Sets the bool "restore" to true when the windows maximized
                }
                else
                {
                    restore = false; //Sets the bool "restore" to false when the windows isn't maximized
                }
            }
        }
    
        private void MaximizeButton_Click(object sender, EventArgs e)
        {
            {
                if (restore == true)
                {
                    this.WindowState = FormWindowState.Normal; //Restore the forms state
                }
                else
                {
                    this.WindowState = FormWindowState.Maximized; //Maximizes the form
                }
            }
        }
    

    Well, i've got three warnings and this is the one i thinks is wrong:

    Field 'WindowsFormsApplication2.Form1.restore' is never assigned to, and will always have its default value false.

    I think it says that the bool "restore" is never used and will always have it's default value FALSE, which it shouldn't because of my set_Restore when it is maximized.

    The other two warnings are:

    The variable 'restore' is assigned but its value is never used The variable 'restore' is assigned but its value is never used

    Thank you in advance.