简体   繁体   中英

Form.Width incorrect on Form.FormClosing()

I am trying to get an accurate Form.Width on FormClosing().

I have a zoom() method I use to scale a picture in the Form. The form's width is then manually re-sized by me to fit. I then, set the MaximumSize's Width limit to the new width of the form. For our purpose, let's say the width and maxwidth are now 1386.

If I re-size the form in the Windows interface by dragging its edge to the left, the width is being reduced just fine. FormClosing reports the re-sized width accurately. Let's say 1107.

However, if go through the zoom() method I wrote, even though I drag to the left to the same position, FormClosing is reporting the ORIGINAL 1386 width. The only things I do to form size in the zoom() method are set width and maxwidth.

I am at a loss to handle this behavior. I need FormClosing to report the correct size. I think FormClosing is setting the Width to MaximumWidth when it fires. I think I read something about how the Form is set to Hidden by the time the FormClosing event is fired. Maybe being Hidden reverts its size to MaxWidth.

Any help would be much appreciated. I have been struggling with this for a few hours and can't find anything.

Thanks.

Here is the relevant code:

 private void zoom()
    {
        //  Re-size form width to slightly more than page width.
        //  This is percentage-based, meaning 100 = default size.

        Size picSize = new Size
        {
            Width = (int)(originalRenderingSize.Width * integerUpDownZoom.Value / 100),
            Height = (int)(originalRenderingSize.Height * integerUpDownZoom.Value / 100),
        };

        // some code here which I commented-out, and still, the problem exists.


        // Set maximum to prevent ugly sizing.
        this.MaximumSize = new Size(picSize.Width + 40, Screen.FromControl(this).WorkingArea.Height);



        // The problem is this:  When this method processes, as shown, a page is rendered and sized according to
        // a user-provided zoom level represented by an integerUpDown control.  Once rendered, the form should
        // re-size its width to match the newly-scaled page's width.  This works, too.  So far, so good.
        //
        // But!!!  If the user grabs the right side of the form and drags it to the left to reduce the width
        // of the form (mind you, this is Windows OS doing this), upon Form.Form_Closing(), you can see a
        // quick flash of the form fully-open and unscrolled.  Then, in the FIRST line of Form_Closing(), 
        // the debugger reports the form's width as the fully-open and UNSCROLLED width.
        //
        //  The goal is to set the width upon the conclusion of this zoom, but then, on closing
        //  get the width of the Form in order to persist it for use the next time the app is run.
        //
        //  2 options have been tried.  Both cause Form_Closing to erroneously report the fully-open width.
        //  But if BOTH are commented-out, the re-sizing occurs and Form_Closing() reports properly the
        //  form's scrolled width.  

        //   Bear in mind that Form_Closing is one of those things that can happen anytime or never.  This means
        //   the bug is triggered by EITHER and ONLY the two options below.

        //  Option 1:  Tried first.  It sets the width fine.
        //  
        //  this.Width = picSize.Width + 40;            

        //  Option 2:  It also sets the width fine.
        //  I think this option causes width to change (it invokes width change, and somewhere in the 
        //  OS's width change, the error occurs.

        //this.MinimumSize = new Size(MaximumSize.Width - 1, this.Height);
        //this.MinimumSize = new Size(0, 0);            
    }

Edit: I have new information which should help. The culprit is FormClosing(). Here's what I did: I re-sized the form in Windows until it occupied maybe 500 pixels in width. The horizontal scrollbar on my panel was showing. Then, in the FormClosing event, I added the line: Form.Show(). After that, I put a MessageBox.Show("Hi."). Sure enough, the Show() caused the form to repaint in its full, unscrolled width. It did not maintain its scroll state.

I think I have the solution and will post back after trying. Namely, I need to check Scroll values and operate on those values as well.

Edit 2: FormClosing() also re-sets HorizontalScroll.Value and VerticalScroll.Value to 0. This is a bummer! I wonder how to work around this. I also looked at e.Cancel, but there seems to be no way to take advantage of it, either. It seems e.Cancel just re-shows() the form in its unhidden state.

Sorry I cannot post the code. It would be pointless anyway. Think about it. The Windows OS handles the resizing. Not me. When the user re-sizes the form, it's all on Windows. If user re-sizes the form to 500 pixels wide, but FormClosing() reports it is 1386 pixels wide, no amount of my coding will show us anything new. This is a behavior issue. There is no code to show you how the Windows OS handles form re-sizing.

On FormClosing Event Use:

MessageBox.Show(this.Width.ToString());

You're using another Instance of your form's class, Form.Show() should not work in this event cause Dispose function is called and if you have referenced the same instance it will be disposed as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM