Count the lines in a label control

jralford

Newcomer
Joined
Jun 18, 2005
Messages
4
Hi,

I'm using Visual C#.net

I would like to know how to get a count of the lines in a label control.
This is easy to do with a text box (GetLineFromCharIndex(99999)), but the label control does not have this function.

The label for instance would be a set width, but the text wraps around over several lines.
If I know the number of lines I can then set the height of the label control appropriately.

Thanks
 
Short of counting the length of the text I don't think you can get the line count because - as you stated - the contents are wraped and there are no line breaks.

The best way is to not set height of the Label, set only the width. This way the height will grow and the width will not.
 
I cannot get the height of the label control to automatically resize, only by setting the width. This worked fine in other languages, like VB6, but does not seem to work in VC#. If you can do this, can you please tell me what properties you are setting for this to happen?

Thanks.


Robby said:
Short of counting the length of the text I don't think you can get the line count because - as you stated - the contents are wraped and there are no line breaks.

The best way is to not set height of the Label, set only the width. This way the height will grow and the width will not.
 
You can create a graphics object and use the Graphics.MeasureString method, which allows you to specify a width (which should be the width of the label). It will return the rectangle that would contain your text, whose height could be divided by the height of a single line to determine the line number.

(I haven't tested this)
Visual Basic:
Function LineCount(ByVal Label As Label) As Integer
    Dim g As Graphics = Label.CreateGraphics
 
    Dim LineHeight As Single = g.MeasureString("X", Label.Font).Height))
    Dim TotalHeight As Single = g.MeasureString(Label.Text, Label.Font, Label.Width).Height
 
    Return CInt(Math.Round(TotalHeight / LineHeight))
End Function
 
Getting Closer

Hi,
Well, Getting very close.
It is not a perfect match. Most of the time the value returned is correct, but sometimes it is off by one line. It seems that usually around the seventh line it looses track and starts returning 1 line less than there should be. It seems the GDI string is not quite the same "format" of the label.
Could it be that the second MeasureString call should include the fourth argument of "StringFormat"?

Any ideas? I'm starting to go nuts over this problem... most of the weekend spent on it!

Thanks for you help!

marble_eater said:
You can create a graphics object and use the Graphics.MeasureString method, which allows you to specify a width (which should be the width of the label). It will return the rectangle that would contain your text, whose height could be divided by the height of a single line to determine the line number.

(I haven't tested this)
Visual Basic:
Function LineCount(ByVal Label As Label) As Integer
    Dim g As Graphics = Label.CreateGraphics
 
    Dim LineHeight As Single = g.MeasureString("X", Label.Font).Height))
    Dim TotalHeight As Single = g.MeasureString(Label.Text, Label.Font, Label.Width).Height
 
    Return CInt(Math.Round(TotalHeight / LineHeight))
End Function
 
Solution.

Sometimes you don't see the wood for the trees...
My end goal was to get the height of the label in order to fit in all the text. Getting the line count was the means to the end. Then I realise the end goal is right there in the code you posted marble_eater. Specifically the value: TotalHeight. So, I don't need the line count at all.

Kudos to you.

James


jralford said:
Hi,
Well, Getting very close.
It is not a perfect match. Most of the time the value returned is correct, but sometimes it is off by one line. It seems that usually around the seventh line it looses track and starts returning 1 line less than there should be. It seems the GDI string is not quite the same "format" of the label.
Could it be that the second MeasureString call should include the fourth argument of "StringFormat"?

Any ideas? I'm starting to go nuts over this problem... most of the weekend spent on it!

Thanks for you help!
 
Back
Top