It sounds like you want the Dock and Anchor properties.
Here is the first link from Google.
It sounds like you want the Dock and Anchor properties.
Here is the first link from Google.
There is something that comes close, try the following: design your Form a bit large and avoid Right and Bottom docked controls. Then some code like this:
private void button1_Click(object sender, EventArgs e)
{
this.AutoSize = true;
this.Size = new Size(10, 10);
}
The AutoSize will find the extend of the Controls.
Here's how I typically handle this case with anchoring: Set the anchor of the labels to Top, Left (the default); The text boxes to Top, Left, Right; and the buttons to Bottom, Right. Size the form in the designer to your desired minimum size. In the designer, set the MinimumSize property of the form to its current size.
At run-time, the user can resize the form. The labels will not move, the text boxes will stretch horizontally, but not vertically. The buttons will move with the right and bottom edges of the form. The user will not be able to make the form (and its controls smaller than the MinimumSize).
Hope this helps.
I handle these sort of things using TableLayoutPanel. TableLayoutPanel lets you layout your controls in columns and rows. Columns and rows have three SizeTypes: AutoSize, Percent, and Absolute. AutoSize sizes the column or row according to the minimum necessary value, Percent sizes the column or row according a percent of unused area, and Absolute sizes the column or row according to an absolute number of pixels.
The VisualStudio designer is helpful. If you would rather write all code by hand, it is still a good idea to play with TableLayoutPanel in the designer and examine the resulting designer code to learn how to operate TableLayoutPanel.
In order to get the minimum necessary size, you should:
Also, you can use ColumnSpan and RowSpan to make a control take up multiple cells.
Once you set things up the way you want them to look, you can use TableLayoutPanel.GetPreferredSize() to determine the minimum required area of the TableLayoutPanel and assign that value to Form.ClientSize.
If the user will be able to resize your form, after you determine the minimum required size you should set the SizeType of the column and row you want to grow with the form to SizeType.Percent and use a value of 100, like this:
// "columnIndex" is the zero based index of the column
this.tableLayoutPanel.ColumnStyles[columnIndex] = new ColumnStyle(SizeType.Percent, 100);
// "rowIndex" is the zero based index of the row
this.tableLayoutPanel.RowStyles[rowIndex] = new RowStyle(SizeType.Percent, 100);
Use the MaximumSize and MinimumSize properties of the form, along with the Anchor properties of the controls.
// make the form 200 pixels high, without possibility to resize the height
// and restrict the minimum width to 200 pixels
this.MinimumSize = new Size(200, 200);
this.MaximumSize = new Size(30000, 200);
Off the top of my head (read: I haven't tested this), I would think you can iterate through all the child controls when the form loads and determine it's minimum size. You can then set the form to this size and set it's MinimumSize
property so it can never be resized to anything smaller.
Something like this, using some LINQ:
OnLoad()
{
int right = this.Controls.Cast<Control>().Max(c => c.Right);
int bottom = this.Controls.Cast<Control>().Max(c => c.Bottom);
// leave a little padding, add maybe 10px or 10%?
int minWidth = right + 10;
int minHeight = bottom + 10;
this.Size = new Size(minWidth, minHeight);
this.MinimumSize = new Size(minWidth, minHeight);
}
About the WinForms font, apparently it's a bug from 1.0 that has been carried over for compatibility's sake. (see here and here.) You can work around it by setting this.Font = SystemFonts.DialogFont
when the form loads. This however doesn't show up in design mode. To work around this, set the font in a BaseForm and derive all your forms from it.
Update: I see how this can be a problem if you have right or bottom-anchored controls. It will use they current position and size and not compute a minimum for size where they won't squish into other controls or become too small. Maybe you can set their anchors programatically after you have resized the form.