Results 1 to 10 of 10

Thread: Anchor & Dock For Automatic Resizing

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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%
    Attached Images Attached Images   

  2. #2

    Re: Anchor & Dock For Automatic Resizing

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

  3. #3
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    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.

  4. #4

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Anchor & Dock For Automatic Resizing

    Quote Originally Posted by kayleigh View Post
    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.

  5. #5
    Member
    Join Date
    Jun 2010
    Posts
    40

    Re: Anchor & Dock For Automatic Resizing

    Is there anything similar to Anchor/Dock in VB6?

  6. #6

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Anchor & Dock For Automatic Resizing

    Quote Originally Posted by IHappenToBe View Post
    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.

  7. #7
    New Member
    Join Date
    Feb 2016
    Posts
    6

    Re: Anchor & Dock For Automatic Resizing

    What if we do have more than 1 button maybe more than 3?
    Name:  Capture.jpg
Views: 10259
Size:  15.0 KB

  8. #8

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Anchor & Dock For Automatic Resizing

    Quote Originally Posted by zackdy View Post
    What if we do have more than 1 button maybe more than 3?
    Name:  Capture.jpg
Views: 10259
Size:  15.0 KB
    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.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    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.

  10. #10

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Anchor & Dock For Automatic Resizing

    Quote Originally Posted by Frank Casey View Post
    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
  •  



Click Here to Expand Forum to Full Width