Groups | Blog | Home
all groups > vb.net upgrade > february 2004 >

vb.net upgrade : Word Wrapping and AutoSize on a label


Steve Goodman
2/29/2004 8:43:57 PM
In VB6 if you had a label control with the properties of Autosize and
Wordwrap set to to TRUE, it would grow the label control, vertically, to
hold all the text. However this functionality does not seem to be included
in VB.Net, does anyone have any idea how to solve this as I have been
pulling my hair out all afternoon.

For example in VB6, place a label control and a command button on a form and
then use this code.

Private Sub Command1_Click()
Label1.AutoSize = True
Label1.WordWrap = True
Label1.Caption = "This is some really long text that in vb6 when you set
the autosize and wordwrap to true it would grow the label box vertically,
but in VB.NET this is not the case."
End Sub

Do I have to create a new label control and overload the onPaint event, or
is there an easier way?

Cheers

Steve.

Steve Goodman
3/2/2004 3:35:10 PM
Ok, finally I have found an answer to my problem, thanks to an article on
microsoft.public.dotnet.languages.vb message board
(http://www.dotnet247.com/247reference/msgs/13/68279.aspx)

It seems that you can use the measureString function on the graphics object
to measure the height of the string, and then all you need to do is set the
label.height to the return value, for example

Label1.Text = "This is some really long text that in vb6 when you set the
autosize and wordwrap to true it would grow the label box vertically, but in
VB.NET this is not the case."

Dim text As String = Label1.Text
Dim textfont As Font = Label1.Font

Dim layoutsize As SizeF = New SizeF(Label1.Width, 5000.0)
Dim g As Graphics = Graphics.FromHwnd(Label1.Handle)
Dim StringSize As SizeF = g.MeasureString(text, textfont, layoutsize)

g.Dispose()

Label1.Height = StringSize.Height

Hope this helps any other people out there with a similar problem.

[quoted text, click to view]

AddThis Social Bookmark Button