Home All Groups Group Topic Archive Search About
Author
29 Nov 2007 7:37 PM
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

Author
29 Nov 2007 8:02 PM
Ignacio Machin ( .NET/ C# MVP )
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Show quote
"randy1200" <randy1***@discussions.microsoft.com> wrote in message
news:8DF2432F-C5A1-4F45-93D4-FCC34215DDE1@microsoft.com...
>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
Author
29 Nov 2007 8:47 PM
Peter Duniho
On 2007-11-29 11:37:01 -0800, randy1200
<randy1***@discussions.microsoft.com> said:

> 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
Author
29 Nov 2007 10:13 PM
Rene
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.

AddThis Social Bookmark Button