Question How to set alignments of form controls dynamically?

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
Hi there,
I have a form containing two panels. In both panels there are two labels and textboxes. I set the 'Center In Form' property of the labels and textboxes to 'Horizontally' and 'Vertically' in design time respectively. But as the form is resizable, when the size of the form is increased or decreased at runtime the controls are no longer center aligned in the panels. How can I handle this problem (provided the main form needs to remain sizable)? Please help. Thanks.
 
Control alignment is handled using the Anchor and or Dock properties of each control. If you have a complex layout then you may need to use one or more TableLayoutPanels and/or FlowLayoutPanels too. If I need to keep something centred, I will generally add a TableLayoutPanel with three columns and/or rows. The content goes in the middle one, which remains a specific size, while the other two are set to occupy 50% each of the remaining space. As the form resizes, the outer columns and/or rows resize by the same amount and the content remains in the middle.

That said, having Labels and TextBoxes centred in a resizable form sounds like a bad idea. Generally, a Label should have its Anchor set to Top-Left, which is the default, so it stays put. A TextBox would also be set to Top-Left but might also include Right so that it will grow and shrink horizontally with its parent.
 
Keeping labels and texboxes centerd in a resizable form was just a short reference. That's not exactly done in my scenario. Just wanted to know the technique. I was exactly looking for this solution. Thanks a lot.
 
Back
Top