CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    [RESOLVED] Determining Label Height

    Is there a way I can determine, at runtime, what the height of a label must be to display the entire label caption.

    The label has a predetermined width, also determined at runtime. The caption that will be displayed will be of variable length so the height must be adjusted so that the full caption can be displayed.

    The font of the label can be changed e.g. different font, size, bold, italic etc. so it must be taken into account.

    So I have available at runtime:
    1. The caption that must be displayed.
    2. The width of the label.
    3. The font, size etc.

    If possible; How can I determine the height of the label?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Determining Label Height

    Here's an old one from a NEW MVP. Welcome!

    http://www.vbforums.com/showthread.php?t=332076
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Determining Label Height

    Will the autosize property do the job you need?

  4. #4
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Determining Label Height

    DataMiser

    The Autosize property makes the label one long line so it's not going to work. I have long captions that must wordwrap to the width of the label but then the height must be adjusted to display the whole caption.

    Dglienna

    I'm going to have a look at the link.

  5. #5
    Join Date
    Apr 2009
    Posts
    394

    Re: Determining Label Height

    check out the forms textwidth function. (You would have to set up the form even if it is temporarily to that of the labels font properties to calculate the width needed to display a string of text. Then based upon that and textheight function, you could set the height of the label.)

    Good Luck

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Determining Label Height

    You could probably do that with the auto size property as well. Use one label with auto size set to true and make it invisible. The label height will be correct for the font and you could calculate the # of lines based on the length. Still not exact as you do not really know where it is going to wrap but I think it could be made to work withotu to much effort.

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Determining Label Height

    The AutoSize property will be a footfall there. The label will not wordwrap, but make one long single line label, except you have put vbCrLf within the string.

    I had a similar problem once, where I had to diplay very long filenames.
    Since in a label no wrapping occurs if there are no blanks somehwere, I chose a textbox to display.
    I use an invisible label to arrange hight.

    I invite you to play with this test I had written for it.
    The txt TextBox is only there to setup the width and position for the LabelText above it.

    If you press the button, some strange strings will be loaded and "autoformatted"
    There is only one example where it fails, when lots of long words mess up the word wrapping.
    Attached Files Attached Files

  8. #8
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Smile Re: Determining Label Height

    The thread Dglienna supplied did the job halfway. It allowed me to determine the textwidth of a string or partial string without having to set the form font to the label font etc.

    Added a bit of code to break up the string into words with the Split function, then loop through the array to build up the strings to make up each line of the label all the while testing the textwidth of the string to see when a new line will occur.

    This way I could determine the number of lines needed and TextHeight * number of lines * Screen.TwipsPerPixelX gave me the height needed for the label. Works while not using a border on the label. With a border the last line are sometimes not displayed.

    Still testing but it seems to work even with changing font type, size, bold etc.

  9. #9
    Join Date
    Apr 2003
    Posts
    1,755

    Re: [RESOLVED] Determining Label Height

    Here's also another way to do it without computing the number of lines needed.
    Code:
    ' module file
    Option Explicit
    
    Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End Type
    Private Const DT_WORDBREAK = &H10&
    Private Const DT_CALCRECT = &H400&
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
       (ByVal hdc&, ByVal lpStr$, ByVal nCount&, lpRect As RECT, ByVal wFormat&) As Long
    
    Public Sub SetLabelCaption(lbl As Label, strText As String)
       Dim rt As RECT, oldFont As StdFont
       rt.Right = lbl.Width \ Screen.TwipsPerPixelX
       Set oldFont = lbl.Parent.Font
       Set lbl.Parent.Font = lbl.Font
       Call DrawText(lbl.Parent.hdc, strText, -1, rt, DT_WORDBREAK Or DT_CALCRECT)
       Set lbl.Parent.Font = oldFont
       lbl.Height = rt.Bottom * Screen.TwipsPerPixelY
       lbl.Caption = strText
    End Sub
    I used DT_WORDBREAK flag so that the function will take care of computing the lines needed. Just pass the Label control and the text you want to be the caption.

    But the problem of bordered caption is still there It is hard to compute the actual border size because it uses a non-standard border that can't be retrieved using GetSystemMetrics. One solution if you want to have a bordered label is to place a bordered PictureBox with the label inside it.

  10. #10
    Join Date
    Dec 2010
    Posts
    2

    Re: [RESOLVED] Determining Label Height

    I am very pleased with the thought and don’t feel like adding anything in it. It a perfect answer.
    ======================
    *********************

  11. #11
    Join Date
    Dec 2010
    Posts
    2

    Re: [RESOLVED] Determining Label Height

    The above statement is seen to be contradictory. The situation is very critical and need an experience complainer to resolve it. Hat’s off. Well done, as we know that “hard work always pays off”, after a long struggle with sincere effort it’s done. This conversation is going no where. It’s lacking the place of a good leader to head the things to come out on conclusion 8) :wink:
    ===============================

    PLR

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Determining Label Height

    Don't add posts to dead threads that you haven't taken part of. Especially with 2 useless comments
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured