Search code examples
vb.netwinformsresizegroupbox

Resize evenly GroupBoxs when resizing form


I have a form (600x500 px). In this form there are 2 groupboxes. The first one being 260 px and the second one 330 px.

What I would like is that when I resize the form, those two boxes resize with it, but by keeping the proportions. (the second one bigger then the first).

No matter how I set the Anchor points, I dont seem to be able to do this.


Solution

  • You can achieve that with a TableLayoutPanel like this:

    • Add a TableLayoutPanel on your Form. Set the anchor to Left, Top, right (and Bottom in needed) or set Dock property to Fill
    • Add two columns on this TableLayoutPanel (by default there is two columns and two rows, so remove the last row)
    • Insert your GroupBoxs to each cell
    • set the Dock property to Fill or Top for each GroupBox
    • By default the Me.TableLayoutPanel1.ColumnStyles is System.Windows.Forms.SizeType.Percent, so just adjust the first column with in designer as you need.

    Result:

    Before resizing enter image description here

    After resizing (image is reduced so you can see that it keeps proportion)

    enter image description here