简体   繁体   English

Form.FormClosing() 上的 Form.Width 不正确

[英]Form.Width incorrect on Form.FormClosing()

I am trying to get an accurate Form.Width on FormClosing().我试图在 FormClosing() 上获得准确的 Form.Width。

I have a zoom() method I use to scale a picture in the Form.我有一个 zoom() 方法用于缩放表单中的图片。 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.然后,我将 MaximumSize 的宽度限制设置为表单的新宽度。 For our purpose, let's say the width and maxwidth are now 1386.为了我们的目的,假设宽度和最大宽度现在是 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.如果我通过向左拖动窗体的边缘来重新调整 Windows 界面中窗体的大小,宽度会减小得很好。 FormClosing reports the re-sized width accurately. FormClosing 准确地报告重新调整大小的宽度。 Let's say 1107.比方说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.但是,如果通过我编写的 zoom() 方法,即使我向左拖动到相同的位置,FormClosing 也会报告原始 1386 宽度。 The only things I do to form size in the zoom() method are set width and maxwidth.我在 zoom() 方法中为形成大小所做的唯一事情是设置宽度和最大宽度。

I am at a loss to handle this behavior.我无法处理这种行为。 I need FormClosing to report the correct size.我需要 FormClosing 来报告正确的大小。 I think FormClosing is setting the Width to MaximumWidth when it fires.我认为 FormClosing 在触发时将 Width 设置为 MaximumWidth。 I think I read something about how the Form is set to Hidden by the time the FormClosing event is fired.我想我读了一些关于如何在 FormClosing 事件被触发时将 Form 设置为 Hidden 的内容。 Maybe being Hidden reverts its size to MaxWidth.也许被隐藏会将其大小恢复为 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().罪魁祸首是 FormClosing()。 Here's what I did: I re-sized the form in Windows until it occupied maybe 500 pixels in width.这就是我所做的:我在 Windows 中重新调整表单的大小,直到它占据大约 500 像素的宽度。 The horizontal scrollbar on my panel was showing.我面板上的水平滚动条正在显示。 Then, in the FormClosing event, I added the line: Form.Show().然后,在 FormClosing 事件中,我添加了以下行:Form.Show()。 After that, I put a MessageBox.Show("Hi.").之后,我放了一个 MessageBox.Show("Hi.")。 Sure enough, the Show() caused the form to repaint in its full, unscrolled width.果然,Show() 导致表单以其完整的、未滚动的宽度重新绘制。 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.也就是说,我需要检查 Scroll 值并对这些值进行操作。

Edit 2: FormClosing() also re-sets HorizontalScroll.Value and VerticalScroll.Value to 0. This is a bummer!编辑 2:FormClosing() 还将 Horizo​​ntalScroll.Value 和 VerticalScroll.Value 重新设置为 0。这太糟糕了! 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.我也看过e.Cancel,但似乎也没有办法利用它。 It seems e.Cancel just re-shows() the form in its unhidden state.似乎 e.Cancel 只是 re-shows() 处于未隐藏状态的表单。

Sorry I cannot post the code.抱歉,我无法发布代码。 It would be pointless anyway.无论如何,这将毫无意义。 Think about it.想想看。 The Windows OS handles the resizing. Windows 操作系统处理调整大小。 Not me.不是我。 When the user re-sizes the form, it's all on Windows.当用户重新调整表单大小时,一切都在 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.如果用户将表单的大小重新调整为 500 像素宽,但 FormClosing() 报告它是 1386 像素宽,那么我的任何编码都不会向我们显示任何新内容。 This is a behavior issue.这是一个行为问题。 There is no code to show you how the Windows OS handles form re-sizing.没有代码可以向您展示 Windows 操作系统如何处理表单大小调整。

On FormClosing Event Use:在 FormClosing 事件中使用:

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.您正在使用表单类的另一个实例,Form.Show() 在此事件中不应该工作,因为 Dispose 函数被调用,如果您引用了同一个实例,它也将被处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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