views:

58

answers:

3

I have 15 to 20 labels with variable text sizes and text boxes arranged in the forms. Text boxes are arranged next to labels. The font and color of the form and hence the form controls can be configured by the user at run time. When I right align the labels and set auto grow property to true and whenever the font style changes (say from Arial to Georgia) the right aligned labels are no more right aligned.

I need a solution on labels (for winforms) to automatically grow to the left when the font size changes.

+1  A: 

Set the anchor to "Right" rather than "Left" (you will probably also have "Top" in which case it's "Right Top" rather than "Left Top"), it should grow in the right (left) direction I believe. Been a while since I did any of this so try it and let me know how it goes.

Lazarus
This appproach is not working.
Nagesh
I see that now that I've had a chance to try it.
Lazarus
There are two solutions I see, the first is simplest and that's the ensure there is sufficient width in your label to accept the longest string you expect to see and then position it correctly with right aligned text. The second is to create a new label class inheriting from the base Label, adding an X2 or RHS int value and then overriding the OnAutoSizeChanged event to ensure that the X position for the label is adjusted accordingly when the width of the label grows.
Lazarus
Thank you very much for the information. I've more or less resolved the problem.
Nagesh
Can you post your solution as an answer to your question here to help others in the future. You can accept your own answer as the correct one too.
Lazarus
@nagesh46s, I wasn't saying to accept my answer. I thought you'd found a solution that resolved the problem for you, whether based on what I said or otherwise. Can *you* add that solution as an answer to your question here and accept that answer?
Lazarus
A: 

You could probably use a TableLayoutPanel.

Daniel Brückner
+1  A: 

You could use also RightToLeft label property instead of modifying Anchor.

serhio