By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Resize label based on contents

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
"Matthew" <tu*************@alltel.net> schrieb:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4

P: n/a
"Matthew" <tu*************@alltel.net> schrieb:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #5

P: n/a
> Set the label's 'AutoSize' property to 'True'.

You're a genius.

Thanks a million!

Matthew
Nov 21 '05 #6

P: n/a
"Matthew" <tu*************@alltel.net> schrieb:
Set the label's 'AutoSize' property to 'True'.


You're a genius.


In future, check a class's properties/methods in documentation before asking
here :-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.