jralford Posted June 18, 2005 Posted June 18, 2005 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 Quote
Moderators Robby Posted June 18, 2005 Moderators Posted June 18, 2005 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. Quote Visit...Bassic Software
jralford Posted June 19, 2005 Author Posted June 19, 2005 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. Quote
Leaders snarfblam Posted June 19, 2005 Leaders Posted June 19, 2005 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 Quote [sIGPIC]e[/sIGPIC]
jralford Posted June 19, 2005 Author Posted June 19, 2005 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 Quote
jralford Posted June 19, 2005 Author Posted June 19, 2005 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! Quote
Moderators Robby Posted June 20, 2005 Moderators Posted June 20, 2005 My appologies, I just realized you're using WinForms, Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.