Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right-aligned labels in WinForms

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?

like image 948
Roman Starkov Avatar asked Nov 07 '09 08:11

Roman Starkov


People also ask

How do you right align a label?

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.

How do I right align a label in asp net?

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.


2 Answers

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.

like image 173
Marc Gravell Avatar answered Sep 19 '22 00:09

Marc Gravell


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.

like image 38
Damien Avatar answered Sep 20 '22 00:09

Damien