Get label height for fixed width

Posted by Jonas on Stack Overflow See other posts from Stack Overflow or by Jonas
Published on 2010-04-13T11:24:31Z Indexed on 2010/04/13 12:43 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

Is there any way I can get the height of a label, if it hypothetically had a certain width? I've been trying with control.GetPreferredSize(size) like so:

Dim wantedWidth as Integer = 100
dim ctrlSize as Size = label.GetPreferredSize(new Size(wantedWidth, 0))

because I thought that setting height = 0, would indicate a free height, but the height I get is way too small. I also tried to estimate the height of the label, using Graphics.MeasureString to calculate the equivalent area of the Label

dim prefWidth as Integer = 100
dim estSize as SizeF = g.MeasureString(label.Text, label.Font)
dim estHeight as Integer = (Integer)(estSize.Width * estSize.Height / prefWidth)

but that yields the same result. Any ideas? I'm on .NET 2.0, unfortunately.

© Stack Overflow or respective owner

Related posts about winforms

Related posts about .net-2.0