-
Oct 17th, 2010, 11:49 PM
#1
Anchor & Dock For Automatic Resizing
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%
Last edited by jmcilhinney; May 8th, 2018 at 10:48 PM.
-
Oct 18th, 2010, 12:02 AM
#2
Re: Anchor & Dock For Automatic Resizing
Great tutorial for newbies who are learning about anchoring controls. Good job JMC.
-
Jan 10th, 2012, 02:50 PM
#3
Fanatic Member
Re: Anchor & Dock For Automatic Resizing
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
Last edited by kayleigh; Jan 10th, 2012 at 02:53 PM.
-
Jan 10th, 2012, 08:22 PM
#4
Re: Anchor & Dock For Automatic Resizing
Originally Posted by 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
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.
Last edited by jmcilhinney; Oct 18th, 2019 at 09:32 AM.
-
Mar 15th, 2012, 12:36 PM
#5
Member
Re: Anchor & Dock For Automatic Resizing
Is there anything similar to Anchor/Dock in VB6?
-
Mar 15th, 2012, 05:36 PM
#6
Re: Anchor & Dock For Automatic Resizing
Originally Posted by IHappenToBe
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.
-
Mar 7th, 2016, 06:38 AM
#7
New Member
Re: Anchor & Dock For Automatic Resizing
What if we do have more than 1 button maybe more than 3?
-
Mar 7th, 2016, 06:51 AM
#8
Re: Anchor & Dock For Automatic Resizing
Originally Posted by zackdy
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.
-
Sep 19th, 2017, 10:32 PM
#9
Registered User
Re: Anchor & Dock For Automatic Resizing
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.
-
Sep 20th, 2017, 04:21 AM
#10
Re: Anchor & Dock For Automatic Resizing
Originally Posted by 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.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|