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

时间:2016-02-04 21:06:32

标签: c# winforms formclosing

我试图在FormClosing()上获得准确的Form.Width。

我有一个zoom()方法,用于缩放表单中的图片。然后由我手动重新调整窗体的宽度以适合。然后,我将MaximumSize的宽度限制设置为表单的新宽度。为了我们的目的,让我们说宽度和最大宽度现在是1386。

如果我通过将其边缘向左拖动来重新调整Windows界面中的表单大小,则宽度正在降低。 FormClosing准确报告重新调整大小的宽度。我们说1107。

但是,如果通过我写的zoom()方法,即使我向左拖动到同一位置,FormClosing也会报告ORIGINAL 1386的宽度。我在zoom()方法中做的唯一一件事就是设置宽度和最大宽度。

我无法处理这种行为。我需要FormClosing来报告正确的大小。我认为FormClosing在触发时将Width设置为MaximumWidth。我想我已经阅读了一些关于FormClosing事件被触发时如何将Form设置为Hidden的内容。也许隐藏会将其大小恢复为MaxWidth。

非常感谢任何帮助。我一直在努力奋斗几个小时,找不到任何东西。

感谢。

以下是相关代码:

 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);            
    }

编辑:我有新的信息应该有所帮助。罪魁祸首是FormClosing()。这就是我所做的:我在Windows中重新调整窗体大小,直到它占用宽度为500像素。我面板上的水平滚动条显示出来。然后,在FormClosing事件中,我添加了一行:Form.Show()。之后,我放了一个MessageBox.Show(“嗨。”)。果然,Show()导致表单以完整的未展开宽度重新绘制。它没有保持其滚动状态。

我想我有解决方案,并会在尝试后回复。也就是说,我需要检查Scroll值并对这些值进行操作。

编辑2:FormClosing()也将Horizo​​ntalScroll.Value和VerticalScroll.Value重新设置为0.这真是一个无赖!我想知道如何解决这个问题。我也看了e.Cancel,但似乎也没有办法利用它。似乎e.Cancel只是重新显示()表单处于未隐藏的状态。

抱歉,我无法发布代码。无论如何,这将毫无意义。想一想。 Windows操作系统处理调整大小。不是我。当用户重新调整表单大小时,它就全部在Windows上。如果用户将表单重新调整为500像素宽,但FormClosing()报告它的宽度为1386像素,则我的编码量不会向我们显示任何新内容。这是一个行为问题。没有代码向您展示Windows操作系统如何处理重新调整大小。

1 个答案:

答案 0 :(得分:0)

在FormClosing事件中使用:

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

你正在使用表单类的另一个实例,Form.Show()不应该在这个事件中工作,因为调用了Dispose函数,如果你引用了同一个实例,它也会被处理掉。