Size forms to fit their contents in C#

[Size forms]

This example shows how to size forms to fit their contents. Figuring out how to size forms can be tricky, particularly if a form contains a MenuStrip with LayoutStyle = Flow so the MenuStrip may hold more than one row of menus.

Fortunately you don’t need to figure out the necessary size yourself. Instead simply set the form’s ClientSize property to the size you want the client area to occupy. The form resizes itself appropriately, allowing room for the menus if necessary.

When you click this program’s button, the following code executes.

// Change the form's size and size it to fit.
private void btnClickMe_Click(object sender, EventArgs e)
    // Move the button to a new location.
    if (btnClickMe.Location.X != 100)
        btnClickMe.Location = new Point(100, 200);
        btnClickMe.Location = new Point(250, 100);

    // Make the form just big enough to hold the button.
    this.ClientSize = new Size(

The code starts by moving its button to a new location. It then sets the form’s ClientSize to be just big enough to display the button.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, forms and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.