Search code examples
c#controlsalignmentwindowstate

Center horizontally and vertically ignores windowstate maximized


As I said in the title center horizontally and verticaly buttons aligns my controls in Visual Studio C# according to form size which I randomly put to see my for clearly ( 751; 469 ) to be exact, it centers the controlls according to that size but my form starts as maximized window as it should be. How can I align the controls as the window maximized?


Solution

  • Most simple will be to change the anchoring, as mentioned here Centering controls within a form in .NET (Winforms)?

    Or

    we can also explore TableLayoutPanel. This is what comes to my mind -

    1. On the main form Control Add a TableLayoutPanel with 3 Column and 3 Row, To make a 3x3 matrix. Set its Dockstyle to Fill

    2. you can configure the width of column & rows in %age so the middle cell of this matrix takes the maximum area, something like this enter image description here

    Something like this enter image description here

    1. Since only one control can be added in a cell, I'll add a panel in the center cell (with Dock style set to Fill), & then I'll add controls to this panel. The anchoring of these controls is set to none.

    this is how it looks at design time

    enter image description here

    And it scales pretty well when you maximize the window at runtime, like this enter image description here

    Hope adding table makes it an easy design. Incase of any issues please write me a comment.