Resize label based on contents

M

Matthew

I have a label that displays the output of my program. I want to make the
label very close to the size of the text it contains. The problem is the
text changes.
So, can I dynamically resize the label based on its contents?
Note: the font size changes as well.

Thank you,

Matthew
 
C

Cor Ligthert

Matthew,

You can try it like this

\\\Most part typed in here so watch typos
Dim g As Graphics = Graphics.FromHwnd(textbox1.Handle)
Dim txtAvgCharWidth As Integer = _
CInt(g.MeasureString("Textbox1.text", textbox1.Font).Width /
Textbox1.lenght)
textbox1.width = mytext.length * txtbAvgCharWidth
////

It is by the way never exact doing this way.

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Matthew said:
I have a label that displays the output of my program. I want
to make the label very close to the size of the text it contains.
The problem is the text changes.
So, can I dynamically resize the label based on its contents?
Note: the font size changes as well.

Set the label's 'AutoSize' property to 'True'.
 
M

Matthew

Set the label's 'AutoSize' property to 'True'.

You're a genius.

Thanks a million!

Matthew
 

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

Top