# VBForums CodeBank > CodeBank - Visual Basic .NET >  Anchor & Dock For Automatic Resizing

## jmcilhinney

A lot of people ask how to get their controls to resize when they resize their form.  The answer is to use the Anchor and/or Dock properties.  For complex layouts, you might also need to use a TableLayoutPanel and/or FlowLayoutPanel.  Here's a quick demo.  Attached are screen shots of a form in the designer at the default 300x300 and then resized to 640x480.  Note that the layout of the controls is maintained.  The values of the Anchor and Dock properties of each control are listed below.

Label1.Anchor = Top, Left
Label1.Dock = None

TextBox1.Anchor = Top, Left, Right
TextBox1.Dock = None

Button1.Anchor = Top, Right
Button1.Dock = None

Label2.Anchor = Top, Left
Label2.Dock = None

ComboBox1.Anchor = Top, Left, Right
ComboBox1.Dock = None

TableLayoutPanel1.Anchor = Top, Bottom, Left, Right
TableLayoutPanel1.Dock = None

TextBox2.Anchor = Top, Left
TextBox2.Dock = Fill

TextBox3.Anchor = Top, Left
TextBox3.Dock = Fill

TextBox4.Anchor = Top, Left
TextBox4.Dock = Fill

TextBox5.Anchor = Top, Left
TextBox5.Dock = Fill

TextBox6.Anchor = Top, Left
TextBox6.Dock = Fill

TextBox7.Anchor = Top, Left
TextBox7.Dock = Fill

Button2.Anchor = Bottom, Right
Button2.Dock = None

Button3.Anchor = Bottom, Right
Button3.Dock = None

In addition, note that the TableLayoutPanel containing the six multi-line TextBoxes is configured to have two columns, each with a width of 50.00%, and three rows, each with a height of 33.33%

----------


## formlesstree4

Great tutorial for newbies who are learning about anchoring controls. Good job JMC.

----------


## kayleigh

Hi john. I stumbled upon this while searching the forums. Would you say it's out side the scope of this thread to ask about anchoring with inside a user control? To make it clearer if i had a mainform that housed a user control. When mainform re-sized it re-sized the user control and controls? The user control does not resize when Mainform does.

Should i make my own thread  :Smilie:

----------


## jmcilhinney

> Hi john. I stumbled upon this while searching the forums. Would you say it's out side the scope of this thread to ask about anchoring with inside a user control? To make it clearer if i had a mainform that housed a user control. When mainform re-sized it re-sized the user control and controls? The user control does not resize when Mainform does.
> 
> Should i make my own thread


Anchoring & docking is relative to a control's immediate parent.  Any child controls in a UserControl will resize based on the UserControl and a UserControl on a form will resize based on the form.  A UserControl is just like any other control.  If you have set the Anchor and/or Dock properties of a UserControl on your form and it doesn't resize with the form then something is broken in your project or your system because a UserControl is a control and it behaves like any other control.

----------


## IHappenToBe

Is there anything similar to Anchor/Dock in VB6?

----------


## jmcilhinney

> Is there anything similar to Anchor/Dock in VB6?


The VB6 forum would be the logical place to ask that question. I've never used VB6 and I'm glad of it.

----------


## zackdy

What if we do have more than 1 button maybe more than 3?

----------


## jmcilhinney

> What if we do have more than 1 button maybe more than 3?


It's a concern that you have to ask that question but I'll answer it anyway.  I have shown how to get six TextBoxes to resize proportionally in three rows and two columns inside a TableLayoutPanel.  If you have more than six controls then you simply add more rows and/or columns to the table.  While performance would be an issue in practice, you could theoretically have thousands of child controls in nested TableLayoutPanels of various sizes and dimensions to create as complex a layout as you want.  All you need to do is to break the layout down into boxes, decide how you want each box to behave and then add and configure the controls appropriately.

----------


## Frank Casey

That is a good example, however I am confused about the 6 multiline textboxes in the TableLayoutPanel.  If they are set to Dockstyle.fill then what is the reason to also set each of their anchor properties to  Top, Left? Seems redundant, I read somewhere else that for any control you either set its docking property or anchoring property not both.

----------


## jmcilhinney

> That is a good example, however I am confused about the 6 multiline textboxes in the TableLayoutPanel.  If they are set to Dockstyle.fill then what is the reason to also set each of their anchor properties to  Top, Left? Seems redundant, I read somewhere else that for any control you either set its docking property or anchoring property not both.


It's quite some time since I created this thread so I don't recall what I was thinking at the time but most likely I set it all up in the designer and then copied those values from the designer code.  Top,Left is the default Anchor value for controls so those for which I've set the Dock are probably just showing their default Anchor value.  It's true that you will only ever want one of Dock or Anchor to take responsibility for the behaviour of the control but they both must always have a value, even if it's None for both.

----------

