1

Answered: See comment section below. Seems I can't post a proper SO answer after this question was marked a duplicate...

Background: Using Winforms and C# Visual Studio 2012, .net 4.5, testing on Win7.

Desired behavior: On "maximize" button, size the main form so that it uses the entire screen except for the task bar, and preserves the title/caption bar.

This is NOT a duplicate. This question is about why this code DOES NOT WORK PROPERLY, despite being posted as a solution in other questions. Thanks!

I coded the following form method:

    private void HSBmainForm_Resize(object sender, EventArgs e)
    {
        // On clicking Maximize, sender is main form, but event is "Empty" so useless...
        // Instead, look at window state and adjust form size as appropriate
        switch(WindowState) {
            case FormWindowState.Maximized: {
                var workArea = Screen.FromControl(this).WorkingArea; // supposedly, screen size minus taskbars and bound toolbars, in pixels
                Debug.WriteLine(workArea);
                // Nope, eliminates desired caption bar and min/max/etc buttons: FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // no border when maximized
                this.MaximumSize = new System.Drawing.Size(workArea.Width, workArea.Height); // supposedly, pixels
              } break;
            case FormWindowState.Normal:
                this.MaximumSize = new System.Drawing.Size(1600, 900); // Minimum workable size for layout of app's forms
                break;
        };
        base.OnLocationChanged(e); // process the event in base class (actually performs the resize)
    }

Unfortunately after maximizing the task bar remains visible as desired, but the main form does not use the entire expected area as shown here (lower right of main form does not go to edge of screen): enter image description here

Any idea what's wrong?

Thanks in advance, Best Regards, Dave

PS: In this test, Windows screen resolution reports 1823 x 978 and workArea reports {X=79,Y=0,Width=1744,Height=963} in debug output.

PPS: The above result is after clicking Maximize the first time. Minimize always works as expected. The second and subsequent Maximize clicks work correctly and use entire screen! Any ideas? Calling the base class event processor twice doesn't make it work on the first attempt...

Dave Nadler
  • 316
  • 4
  • 15
  • Uhm... doesn't maximizing the form ensure that it will use the whole screen workspace without the task bar? – IS4 Aug 07 '17 at 00:04
  • This is NOT a duplicate. This question is about why this code DOES NOT WORK PROPERLY, despite being posted as a solution to other questions and in places other than SO. Thanks! – Dave Nadler Aug 07 '17 at 00:13
  • @IllidanS4, please see the desired behavior, Thanks! – Dave Nadler Aug 07 '17 at 00:15
  • I am still not sure what behaviour this code should have. What if you don't touch the MaximumSize? Will the form be properly maximized then? – IS4 Aug 07 '17 at 00:20
  • @IllidanS4, if the form's maximum size is not updated, the framework will not increase the form's size (because the starting maximum is less than the screen size). – Dave Nadler Aug 07 '17 at 00:23
  • @Enigmativity, please note this is NOT a duplicate, Thanks! – Dave Nadler Aug 07 '17 at 00:24
  • Why don't you set the *MaximumSize* to `new Size(Int32.MaxValue, Int32.MaxValue)`? There won't be any practical limit then. – IS4 Aug 07 '17 at 00:29
  • Answer to the question: There is no reason for any MaximumSize to be set for the main form (and I have no idea why it was set for this application). Removing the MaximumSize setting (in designer) and removing the above event handler causes maximize to work properly the first time. Thanks to @IllidanS4 I noticed this. No idea how to post a proper SO answer after this question was marked a duplicate... – Dave Nadler Aug 07 '17 at 01:01
  • @DaveNadler - I've re-open. Please don't just say "this is NOT a duplicate". Explain why if you want it re-opened. – Enigmativity Aug 07 '17 at 03:26
  • 1
    Normally, if registering for an event (here Form.Resize), it is neither neccessary nor recommended to call the base class method that raises the event. By the way, you're calling NOT Form.OnResize(), that a would expect, but Form.OnLocationChanged()??? – KBO Aug 07 '17 at 05:24

1 Answers1

0

This problem was because MaximumSize was set. There is no reason for MaximumSize to be set for the main form (and I have no idea why it was set for this application). Removing the MaximumSize setting (in designer) and removing the above event handler causes maximize to work properly (per the desired behavior above).

The WinForms framework does substantial event processing prior calling your event handler, including setting WindowState to Maximized and setting the form size to the MaximumSize (if specified) or the WorkingArea size (if MaximumSize is not specified). This is upside-down from many frameworks, where your event handler gets called first and you are expected to call the base class event processor... Hence the bizarre behavior where Maximize worked as expected the 2nd time (because the first time reset MaximumSize, which was used the 2nd time)...

Dave Nadler
  • 316
  • 4
  • 15