Search code examples
c#winformsvisual-studioalignment

How to align two different labels to center? (Windows Form App)


I have two labels on my screen. One must be a little higher than another. I can't combine them to one because of different formatting, so I set Dock property to Fill and its not working (one label overlaps another).


Solution

  • The TextAlign property on the label will align the text with respect to its own size. If AutoSize is set to true the size will be as small as possible so you won't see any changes for alignment.

    Try setting AutoSize to false, TextAlign to MiddleCenter, then either resize/position your controls as you like (bad if the parent control resizes), or try setting Dock to Top or Bottom.

    If you want more control over the vertical position you could try putting your Labels in a TableLayoutPanel, then you set Dock to Fill for each label in their separate cells.