Monday, November 9, 2009

Center Align Controls in Windows Forms in .NET

When you are creating the windows forms application, It is hard to center align the control or set of controls in the windows form. The workout for this is pretty simple. Add a TableLayoutPanel control. It should have only one row with height equal to 100% and one column with width equal to 100%. In the properties section change the Dock property to Fill. Now add the desired control to form or add a container control like FlowLayoutPanel,GroupBox, Panel, TableLayoutPanel, TabControl etc and add controls to it. Change the Anchor property to single control or container to None. This will display all the single control or all the controls added to that container aligned in the center of the form even when the form is resized.
I am sure that there may be other ways around to this problem but this is so far the simplest I discovered while playing with the code.

No comments:

Post a Comment