Determine proper width of Label?

  • Thread starter Thread starter David Sworder
  • Start date Start date
D

David Sworder

Hi,

A Windows form that I'm developing will have two controls: Label1 and
Label2. Both controls are of type Label (go figure). The text of Label1 will
not be known until runtime and may change at times. When the MyText property
of the containing form is set, I want to:

a) Set the Label1.Text property appropriately.
b) Change the width of Label1 so that it is exactly wide enough to show
the text.
c) Move Label2 so that its left edge is equal to Label1's right edge +
1.

Steps (a) and (c) are easy, but I'm not sure how to approach step (b).
Given a string, how do I set the width of Label1 to a value that is exactly
large enough to show the string?

David
 
Thanks, guys.. I had no idea that the Label.AutoSize property even
existed. I was just about to implement my own solution using
Graphics.MeasureString(), but clearly setting AutoSize to true is the way to
go. Thanks again!

David
 
Back
Top