Search code examples
c#winformsdocking

Dock control to other one in WinForms


I have got MainForm with TabPage. On TabPage i have got three groupboxes next to one another. First is docked to left top, third to right top. everything is ok but if i maximize window , center textbox goes to bad position (is hide behing first groupbox) How dock this one to stay always between first and third groupbox?


Solution

  • You can accomplish this by using the Dock properties OR the anchor properties. Adding the groups boxes so they are ordered left to right:

    Using the Dock properties:

    `groupBox1.Dock = left`
    `groupBox3.Dock = right`
    `groupbox2.Dock = center`
    

    Using the Anchor properties:

    `groupBox1.Anchor = Top, Left`
    `groupBox3.Anchor = Top, Right`
    `groupbox2.Anchor = Top, Left, Right`
    

    EDIT: In either case the center groupBox (groupBox2) should adjust to fill the space between the two outer groupboxes (groupBox1 and groupBox3)