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?
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.
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.)
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.
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.
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.
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.
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:
===============================
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.