I am making a Windows Forms application. I want the forms height to increase after a button is pressed. How do I do this?
C# WinForms – How to Resize a Windows Form
c++resizewindow-resizewinforms
Related Solutions
You should set the Anchor
and Dock
properties on the controls in the forms.
The Anchor
property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor
to Bottom
, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor
to Top | Bottom
, the control will resize vertically as you resize the form.
To make a control resize with the form, set the Anchor
to all four sides, or set Dock
to Fill
.
Windows form does not provide any feature to do this. But, you can write your own code and make your form resolution independent.
This is not a complete example to make windows form resolution independent but, you can get logic from here. The following code creates problem when you resize the window quickly.
CODE:
private Size oldSize;
private void Form1_Load(object sender, EventArgs e) => oldSize = base.Size;
protected override void OnResize(System.EventArgs e)
{
base.OnResize(e);
foreach (Control cnt in this.Controls)
ResizeAll(cnt, base.Size);
oldSize = base.Size;
}
private void ResizeAll(Control control, Size newSize)
{
int width = newSize.Width - oldSize.Width;
control.Left += (control.Left * width) / oldSize.Width;
control.Width += (control.Width * width) / oldSize.Width;
int height = newSize.Height - oldSize.Height;
control.Top += (control.Top * height) / oldSize.Height;
control.Height += (control.Height * height) / oldSize.Height;
}
Otherwise you can use any third party control like DevExpress Tool. There is LayoutControl which is providing same facility. you can show and hide any control at runtime without leaving blank space.
Best Answer
Use the
Height
property. For instance: