How do you resize a form to fit its content automatically?

Use the AutoSize and AutoSizeMode properties.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.autosize.aspx

An example:

private void Form1_Load(object sender, EventArgs e)
{
    // no smaller than design time size
    this.MinimumSize = new System.Drawing.Size(this.Width, this.Height);

    // no larger than screen size
    this.MaximumSize = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, (int)System.Windows.SystemParameters.PrimaryScreenHeight);

    this.AutoSize = true;
    this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

    // rest of your code here...
}

Leave a Comment