Resize Controls with Form Resize

42,832

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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);
Share:
42,832
Kairan
Author by

Kairan

Updated on July 09, 2022

Comments

  • Kairan
    Kairan almost 2 years

    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.

  • Mohammad Dehghan
    Mohammad Dehghan over 11 years
    He states that he tried anchors/docks already. Also the question implies that he want all the controls to scale uniformly. So this is not the answer to the question.
  • Freddie Fabregas
    Freddie Fabregas over 11 years
    He might use the anchor / docks. The question is: did he used it correctly?
  • Kairan
    Kairan over 11 years
    I tried anchor and dock and it seems to only "move" the controls around on resize, but not actual resize the controls