Is the size of a Form in Visual Studio designer limited to screen resolution?

Solution 1:

Unfortunately (I hope someone else will post a better solution!), the only workaround I'm aware of is to place a panel inside the form.

Set the Autoscroll and AutoSize properties of the Parent Form to true. Then increase the panel size to the desired size. The form itself will still not get any larger than your screen resolution, but it will show scroll bars, so at least you can use the designer to drop controls etc beyond your size limitations onto the larger panel.

Then, you may need to add some code to adjust the the forms size at run-time so that it is large enough to show the panel without scroll bars (and perhaps also disable the Autoscroll property).

I know, It's not a particularly nice workaround...

EDIT:

Looks like this is intentional and by design:

MSDN

Property Form.Size: The maximum value of this property is limited by the resolution of the screen on which the form runs. The value cannot be greater than 12 pixels over each screen dimension (horizontal + 12 and vertical + 12).

and again at Microsoft Connect/Public Bug Tracking:

Posted by Microsoft on 10/9/2008 at 12:18 AM

Thanks for your feedback on the .NET Framework!

The issue that you have reported is actually By Design.

In MSDN at http://msdn.microsoft.com/en-us/library/25w4thew.aspx, you can find the following information at the topic Form.Size Property:

The maximum value of this property is limited by the resolution of the screen on which the form runs. The value cannot be greater than 12 pixels over each screen dimension (horizontal + 12 and vertical + 12).

Therefore, we can't enlarge our forms indefinitely. This behavior is consistent with other software, such as Notepad and Microsoft Paint.

This behavior is defined in the mothed Form.SetBoundsCore(...) with the following code:

Size max = SystemInformation.MaxWindowTrackSize;

if (height > max.Height) {

height = max.Height; }

if (width > max.Width) {

width = max.Width; }

[...]

Thanks, UIFx Team

EDIT2:

Since the check is hardcoded in Forms.SetBoundsCore like (using ILSpy as a decompiler):

if (this.WindowState == FormWindowState.Normal && (base.Height != height || base.Width != width))
    {
        Size maxWindowTrackSize = SystemInformation.MaxWindowTrackSize;
        if (height > maxWindowTrackSize.Height)
        {
            height = maxWindowTrackSize.Height;
        }
        if (width > maxWindowTrackSize.Width)
        {
            width = maxWindowTrackSize.Width;
        }
    }

and SetBoundsCore is a protected function, perhaps you could try deriving a class from Windows.Forms.Form, override SetBoundsCore and don't enforce this check in your version of SetBoundsCore? I haven't tried if it works though...

Solution 2:

This worked for me, copied from

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class Form1 : Form
{
    [DllImport("User32.dll", CharSet = CharSet.Ansi, SetLastError = true,    ExactSpelling = true)]
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool Repaint);

    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        this.MaximumSize = new Size(5000, 800);
        bool Result = MoveWindow(this.Handle, this.Left, this.Top, 5000, 500, true);
    }
    public Form1()
    {
        Load += Form1_Load;
    }

}

Solution 3:

Deriving from Form, overriding some properties and using interop. This is VB.NET sorry but you get the idea.

Using the derived form you can use "SizeDesign" and "SizeRuntime" properties for design and runtime respectively.

 Imports System.Windows.Forms
Imports System.ComponentModel

Public Class FormEx
    Inherits Form

    Private Declare Function MoveWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal Repaint As Boolean) As Boolean

    Private Const DEFAULTSIZE_X = 1024
    Private Const DEFAULTSIZE_Y = 768

    Protected Overrides Sub OnHandleCreated(e As System.EventArgs)
        MyBase.OnHandleCreated(e)

        If mSizeRuntime = System.Drawing.Size.Empty Then
            SizeRuntime = New System.Drawing.Size(DEFAULTSIZE_X, DEFAULTSIZE_Y)
        End If

        If mSizeDesign = System.Drawing.Size.Empty Then
            SizeDesign = New System.Drawing.Size(DEFAULTSIZE_X, DEFAULTSIZE_Y)
        End If
    End Sub

    <Browsable(False)> _
    Public Shadows Property Size As System.Drawing.Size

    Private mSizeDesign As System.Drawing.Size = System.Drawing.Size.Empty
    <Category("Layout"), _
    Description("Sets the size of the form at design time."), _
    RefreshProperties(RefreshProperties.All)> _
    Public Property SizeDesign As System.Drawing.Size
        Get
            Return mSizeDesign
        End Get
        Set(value As System.Drawing.Size)
            mSizeDesign = value
            If Me.DesignMode Then
                MoveWindow(Me.Handle, Me.Left, Me.Top, value.Width, value.Height, True)
            End If
        End Set
    End Property

    Private mSizeRuntime As System.Drawing.Size = System.Drawing.Size.Empty
    <Category("Layout"), _
    Description("Sets the size of the form at runtime."), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property SizeRuntime As System.Drawing.Size
        Get
            Return mSizeRuntime
        End Get
        Set(value As System.Drawing.Size)
            mSizeRuntime = value
            If Not Me.DesignMode Then
                MyBase.Size = value
            End If
        End Set
    End Property

End Class

A.J.