Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Controls with Form Resize

I have read several stack overflow questions without finding a good working solution to my problem. How can I resize my controls whenever the form is resized? I would like them to get larger or smaller when the form becomes larger or smaller.

In visual basic this was quite easy to do with the form.Zoom property (which did't really require resizing controls of course, but solved what I needed). Unfortunately this is not available in C# winforms.

Here is some other things I have tried without luck:

private void formMain_Resize(object sender, EventArgs e)
{/*
double scale;
this.scaleWidth = (float)this.Width / (float)this.origWidth;
this.scaleHeight = (float)this.Height / (float)this.origHeight;
if (this.scaleHeight > this.scaleWidth)
{
    scale = this.scaleHeight;
}
else
{
    scale = this.scaleWidth;
}

foreach (Control control in this.Controls)
{
    control.Height = (int)(control.Height * this.scaleHeight);
    control.Width = (int)(control.Width * this.scaleWidth);
    this.Refresh();
    //  control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
///////This scaling didnt work for me either
//this.Scale(new SizeF(this.scaleWidth, this.scaleHeight));
//this.Refresh();
*/
}

If I overlooked an actualy working sample of code on another stack overflow question I would love to see it, but the ones I found were similar to those above which are not working.

Perhaps I was misusing it and someone could post sample code to show for those of us who keep asking this question how to go about solving the problem.

Also, I have tried using some of the anchor/docking tools thinking they would automatically allow it but it didn't.

like image 291
Kairan Avatar asked Feb 28 '13 09:02

Kairan


People also ask

How do you resize a control within the form window?

To resize a controlSelect the control and press the arrow keys while holding down the Shift key to resize the control one pixel at a time. Press the down arrow or right arrow keys while holding down the Shift and Ctrl keys to resize the control in large increments.

How do I resize a form in Access?

Click on the Format tab to filter the properties. This can make it easier to find the required properties. You can also resize the controls by clicking and dragging their edges if you wish. However, for more control over the sizing, use the Property Sheet.

How do I make Winforms resizable?

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill . It's fairly intuitive once you start using it. For example, if you have OK/Cancel buttons in the lower right of your dialog, set the Anchor property to Bottom and Right to have them drag properly on the form.


1 Answers

The best option is to use a TableLayoutPanel. Put TableLayoutPanel on the form, set the Dock property to Fill, create required rows and columns and put the controls inside the cells. Of course you need to set Dock/Anchor on the controls inside the cells, so they respond to changes to the cell size. In some situations you may need to put a Panel into a cell and drop the controls inside it, because every cell can only contain a single control. You may also need to set RowSpan/ColumnSpan on the controls.

By using a TableLayoutPanel, you have complete control over how your cotrols should be arranged. You can set absolute or percentage size for rows and columns.

like image 94
Mohammad Dehghan Avatar answered Sep 23 '22 05:09

Mohammad Dehghan