Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Moderators
Posted

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.

Visit...Bassic Software
Posted

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.

 

 

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.

  • Leaders
Posted

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)

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

[sIGPIC]e[/sIGPIC]
Posted

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!

 

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)

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

Posted

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

 

 

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...