Resize Controls with Form Resize

2019-01-09 11:10发布

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.

5条回答
孤傲高冷的网名
2楼-- · 2019-01-09 11:13

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.

查看更多
戒情不戒烟
3楼-- · 2019-01-09 11:26

What you are trying to do in your code is to change the sizes of the controls which isn't so good approach. Generally, the size of the Buttons and TextBoxes shouldn't be changed when you re-size your form, but they often need to move (change location). Some controls do need to change size according to the re-sized form and but in most cases only one dimension. The central controls that are used for working area (if you are developing the tool for drawing for instance) should change sizes of both dimensions. All this you can accomplish by properly setting Dock and/or Anchor properties of the controls.

textBox1.Dock = DockStyle.Bottom;
textBox1.Anchor = AnchorStyles.Bottom & AnchorStyles.Left;

All these are also easily set in the Properties panel when using designer.

But if that isn't enough for you, in rare cases, you will most definitely want to only change the location of the control:

textBox1.Location = new Point(newX, newY);
查看更多
祖国的老花朵
4楼-- · 2019-01-09 11:28

Use Anchor of the control. There's an option on anchoring the top, bottom, left and right. And you're good to go.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-09 11:28

I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.

Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.

I would point out I am aware that some folks online mention split containers use lots of resources.

查看更多
We Are One
6楼-- · 2019-01-09 11:40

If your controls are in a group box, be sure to set the group boxes properties to resize. Controls inside the box are controlled by the box. The box size (unless it is inside another box) is controlled by the form.

查看更多
登录 后发表回答