Label Expand Left

  • Thread starter Thread starter randy1200
  • Start date Start date
R

randy1200

I have two labels on a form. The text for these two labels is assigned at
runtime.

lblLeft lblRight

If the text for lblLeft is wider than the default, the text runs into
lblRight.

How do I make lblLeft automatically expand to the left?

Thanks,
Randy
 
Hi,

I'm not sure if this is doable, first take a look at Label.RightToLeft.
Otherwise you will have to calculate the position of label 2. using
Label.Width (or Graphics.MeasureString) you can determine the width lf
lblLeft and then position lblRight accordingly.
 
I have two labels on a form. The text for these two labels is assigned at
runtime.

lblLeft lblRight

If the text for lblLeft is wider than the default, the text runs into
lblRight.

How do I make lblLeft automatically expand to the left?

It depends on what behavior exactly you need.

But one solution would be to set the left label to be right-aligned
text, and then position the two controls so that they are flush against
each other (right edge of left label against left edge of right label).
Then the left label's text will always be positioned starting far
enough at the left to always just reach the right label's text.

If you want something more complicated, then you will probably have to
write some code to explicitly position the left label based on its
width, as Igancio suggests.

You _might_ be able to do something by creating a UserControl that
contains your two labels and is the same size as whatever area within
your form you want those two labels to occupy at most, and then doing
something with the Anchor and Dock properties of the labels so that the
left label always fills whatever space is remaining according to your
needs. But I haven't given that much thought, have never tried it
myself, and it could be a wild goose chase trying to do it that way. :)

Pete
 
Assuming you don't want to add custom code and that this is a regular
windows form, you may want to take a look at the "TableLayoutPanel control".
You can configure that thing so that the label wraps in multiple line if it
does not fit on the table cell or make the cell grow with the label pushing
the label on the right without overlapping.

Not sure if this would do what you want but you may want to give it a shoot.
 
Many thanks for all the responses.

I tried the suggestions on setting Properties such as right to left and
TextAlign, but when the label text was reassigned at runtime, the text did
not expand to the left as I had hoped.

Measuring the new text lengh and repositioning seems like a viable solution.

The tableLayoutPanel solution seemed simplier, so I went with that.

Thanks again,
Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top