The most obvious way to right-align a Label
in WinForms doesn't work: setting anchor to Top/Bottom Right and TextAlign to TopRight. If the text changes the label's Left coordinate remains unchanged instead of the Right coordinate (which, one might argue, is a bug).
For this reason I've always used a full-width TableLayoutPanel
for right-aligned labels. However this is not always very convenient, depending on the layout in question...
So, I wonder if there are any other ways to keep a Label right-aligned in WinForms that never occurred to me?
We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.
Put the label in a control on the page which has a specific alignment set. If your creating the label in the code behind then create a control with the specific alignment that can have a label inserted into it programmatically.
One simple option is to disable AutoSize
(set to false
) and over-size it so there is spare space.
Alternatively, perhaps use Dock
instead of just Anchor
, although this has a different meaning, so you may need to put it in a Panel
or similar). Ultimately this works like the first - by over-sizing it in the first place; so perhaps the first option is simpler.
Using a TableLayoutPanel with docked labels is the only reliable method that I've found for placing right-aligned labels in Winforms. Turning off AutoSize and using oversized labels seems to cause strange anomalies for High DPI users.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With