Export (0) Print
Expand All

Form.AutoSize Property

Resize the form according to the setting of AutoSizeMode.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(true)]
public override bool AutoSize { get; set; }

Property Value

Type: System.Boolean
true if the form will automatically resize; false if it must be manually resized.

Use AutoSize to force a form to resize to fit its contents.

A form does not automatically resize in the Visual Studio forms designer, regardless of the values of the AutoSize and AutoSizeMode properties. The form correctly resizes itself at run time according to the values of these two properties. By contrast, a custom UserControl automatically resizes itself both at design time and at run time.

When using AutoSize, the MinimumSize and MaximumSize properties are respected, but the current value of the Size property is ignored. Using AutoSize and AutoSizeMode also renders the AutoScroll property superfluous, as there is no way to shrink the form to hide its contained controls from view.

See the AutoSizeMode enumeration for information on how a form behaves when AutoSize is true.

The following code example shows a form created using code that automatically resizes to fit its contents. When run, the form displays a Label, a TextBox for entering a URL, and a Button for displaying that URL inside of the user's default Web browser. The code example uses a FlowLayoutPanel to lay out the contained controls one after the other. It also sets the AutoSize and AutoSizeMode to grow and shrink to fit the contents of its form.

private void Form1_Load(object sender, EventArgs e)
{
    this.AutoSize = true;
    this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    this.Text = "URL Opener";

    flowPanel = new FlowLayoutPanel();
    flowPanel.AutoSize = true;
    flowPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    this.Controls.Add(flowPanel);

    urlLabel = new Label();
    urlLabel.Name = "urlLabel";
    urlLabel.Text = "URL:";
    urlLabel.Width = 50;
    urlLabel.TextAlign = ContentAlignment.MiddleCenter;
    flowPanel.Controls.Add(urlLabel);

    urlTextBox = new TextBox();
    urlTextBox.Name = "urlTextBox";
    urlTextBox.Width = 250;
    flowPanel.Controls.Add(urlTextBox);

    urlButton = new Button();
    urlButton.Name = "urlButton";
    urlButton.Text = "Open URL in Browser";
    urlButton.Click += new EventHandler(urlButton_Click);
    flowPanel.Controls.Add(urlButton);
}

void urlButton_Click(object sender, EventArgs e)
{
    try
    {
        Uri newUri = new Uri(urlTextBox.Text);
    }
    catch (UriFormatException uriEx)
    {
        MessageBox.Show("Sorry, your URL is malformed. Try again. Error: " + uriEx.Message);
        urlTextBox.ForeColor = Color.Red;
        return;
    }

    // Valid URI. Reset any previous error color, and launch the URL in the  
    // default browser. 
    // NOTE: Depending on the user's settings, this method of starting the 
    // browser may use an existing window in an existing Web browser process. 
    // To get around this, start up a specific browser instance instead using one of 
    // the overloads for Process.Start. You can examine the registry to find the 
    // current default browser and launch that, or hard-code a specific browser.
    urlTextBox.ForeColor = Color.Black;
    Process.Start(urlTextBox.Text);
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft