How to auto-size the height and width of a c# windows form? So when I maximize the form all its compo开发者_高级运维nents are fit to the screen?
use the Anchor
property of each control, to snap it to either end of the containing form. then when you resize the form, those anchored controls are resized as well.
In addition to Anchor, there is also a dock property. This will auto size the controls by docking to one or more sides of the container the control is in, if a control is docked to all sides then it will be 'maximized' to fill its container.
You can set the minimum and maximum size of form as shown below
this.MinimumSize = new Size(140, 480);
this.MaximumSize = new Size(140, 480);
You can also use it as below
private void Form1_Load(object sender, EventArgs e)
{
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w, h);
}
Another way it can work for you is the
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
this.Location = new Point((screen.Width - w) / 2, (screen.Height - h) / 2);
this.Size = new Size(w, h);
精彩评论