Nagesh
Nagesh

Reputation: 1308

Resizing labels in Winforms to Left

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.

Upvotes: 5

Views: 8697

Answers (5)

nathant23
nathant23

Reputation: 113

I had the same problem. My fix was to create a simple function to move the label, which I called after any event or code that changed the label size.

Enter the name of the label you want to grow left, and the X coordinate of the right end of the label. Then you call this function after any changes to the label.

private void repositionLabel(Label lab, int endPoint)
        {
            lab.Left = endPoint - lab.Width; 
        }

For example, you have a label named myLabel positioned at (75,75) and it currently has a width of 25 and you always want it to end at (100,75). Then when this happens:

myLabel.Text = "blah blah blah blah blah blah";

you then follow the text change with:

repostitionLabel(myLable, 100);

This will make it look like the label expanded to the left.

Upvotes: 1

bmkorkut
bmkorkut

Reputation: 616

Set your label properties as folowing;

AutoSize = false;
TextAlign = TopRight (or anything to right)

Then manually resize your label to a maximum area to fit your longest text. That worked for me.

Upvotes: 7

serhio
serhio

Reputation: 28586

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

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59645

You could probably use a TableLayoutPanel.

Upvotes: 1

Lazarus
Lazarus

Reputation: 43064

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.

Upvotes: 3

Related Questions