Previous | Index | Next 

[PRB] The ClientSize property of a form can return an incorrect value

If you modify a migrated .NET form without a control box (i.e. ControlBox=False) inside the Visual Studio designer, then at runtime the ClientSize property will return an incorrect value that differs from the value stored in the code-behind portion of the form. There are two ways to solve this anomaly.

First, you can force the correct form size in the Form_Load method:

    ' VB.NET
    Private Sub Form_Load() Handles MyBase.Load
           Me.ClientSize = New System.Drawing.Size(, )
    End Sub

    // C#
    private void Form_Load() 
    {
           this.ClientSize = new System.Drawing.Size(, );
    }

Second, you can override the OnResize method as follows:

    ' VB.NET
    Protected m_PreClientSize As Size

    Protected Overrides Sub OnResize(e As EventArgs)
           If (MyBase.BorderStyle = VBRUN.FormBorderStyleConstants.vbFixedDouble OrElse
           MyBase.BorderStyle = VBRUN.FormBorderStyleConstants.vbSizableToolWindow) 
           AndAlso MyBase.ControlBox = False AndAlso 
           (MyBase.Location.X <> 0 OrElse MyBase.Location.Y <> 0) Then
                   MyBase.ClientSize = m_PreClientSize
                   Exit Sub
           End If
           
           m_PreClientSize = MyBase.ClientSize
           MyBase.OnResize(e)
    End Sub

    // C#
    protected Size m_PreClientSize As Size

    Protected Overrides void OnResize(EventArgs e)
    {
           if ((base.BorderStyle == VBRUN.FormBorderStyleConstants.vbFixedDouble ||
           base.BorderStyle == VBRUN.FormBorderStyleConstants.vbSizableToolWindow)
           && base.ControlBox == false &&
           (base.Location.X != 0 || base.Location.Y != 0) 
           {
                  base.ClientSize = m_PreClientSize;
                  return;
            }

            m_PreClientSize = base.ClientSize;
            base.OnResize(e);
    }

 

Previous | Index | Next