Determine height of text wrapped in label

  • Thread starter Thread starter John Baro
  • Start date Start date
J

John Baro

If I have a label (AutoSize = false) with more text than can fit it wraps
downwards.
I need to determine how high the label needs to be to fit the whole of the
text in.
I can then put it into a scrolling panel and scroll it like a textbox.
Any ideas on how to do this or a more appropriate method are muchly
appreciated.

Cheers
JB
 
Hi, John

you can use Graphics.MeasureCharacterRanges and Graphics.MeasureString to
get necessary measurements.
MeasureString is useful when you don't need high precision - however for big
strings it is not too good. Some people recommend to use always range
function.

HTH
Alex
 
Thanks Alex
I wanted to try and avoid that as it is quite complicated and the label
control (generally) does a good job of wrapping its text.
I ended up subclassing the label control, adding a new AutoSize enum and
resizing the control using preferredwidth and preferredheight.
It is reasonably accurate except when I have really narrow labels and will
have to do for now.
Cheers
JB
 

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

Similar Threads

Label Height 1
System.Windows.Forms.Label 11
SubForm Label Height 3
how to vertically autosize a mutliline text box 3
TEXT word wrap 2
Multi-line GroupBox text? 3
word wraping label 1
Stop Labels Wrapping Text 1

Back
Top