VB icon

Autosize a Label Caption

Email
Submitted on: 1/5/2015 8:38:00 PM
By: Geoff Temple (from psc cd)  
Level: Intermediate
User Rating: By 6 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 471
 
     This small and very simple sub will format the caption of a Label control if the text is too big to display in the control. The sub will trucate the text and append "..." to the end of the text (indicating to the user that they are not seeing the full text). VB automatically wordwraps the caption of a label if it is too big, however, this results in the caption being truncated only where there is a space. Also, you can see the top of the next line of the caption. Example Make and Model: Cadillac becomes: Make and Model: Cadillac Eldor... I find this extremely useful when I don't know the maximum length of the text the label will contain, or if I don't have enough screen real estate to make the Label big enough. Just pass a label to this sub for formatting.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Autosize a Label Caption
' Description:This small and very simple sub will format the caption of a Label control if the text is too big to display in the control. The sub will trucate the text and append "..." to the end of the text (indicating to the user that they are not seeing the full text). VB automatically wordwraps the caption of a label if it is too big, however, this results in the caption being truncated only where there is a space. Also, you can see the top of the next line of the caption.
Example
Make and Model: Cadillac
becomes:
Make and Model: Cadillac Eldor...
I find this extremely useful when I don't know the maximum length of the text the label will contain, or if I don't have enough screen real estate to make the Label big enough.
Just pass a label to this sub for formatting.
' By: Geoff Temple (from psc cd)
'
' Inputs:A label control.
'**************************************

'This small and very simple sub will format the
'caption of a Label control if the text is too
'big to display in the control. The sub will
'trucate the text and append "..." to the end
'of the text (indicating to the user that they
'are not seeing the full text). VB automatically
'wordwraps the caption of a label if it is too
'big, however, this results in the caption being
'truncated only where there is a space. Also,
'you can see the top of the next line of the caption.
'Example
'Make and Model: Cadillac
'becomes:
'Make and Model: Cadillac Eldor...
'I find this extremely useful when I don't know the
'maximum length of the text the label will contain,
'or if I don't have enough screen real estate to
'make the Label big enough.
Private Sub AutoSizeCaption(lbl As Label)
Dim iAs Integer
Dim iLabelWidth As Integer
Dim sTextAs String
Const kMore = "..."
' store orignal caption and width
sText = lbl.Caption
' numeric or date? Don't format.
If IsNumeric(lbl.Caption) Or IsDate(lbl.Caption) Then Exit Sub
iLabelWidth = lbl.Width
' allow label to "spring" to it's actual width
lbl.AutoSize = True
' is required width of label < actual width?
If lbl.Width > iLabelWidth Then
i = Len(sText) - 1
Do
lbl.Caption = Left(sText, i) & kMore
i = i - 1
Loop Until (lbl.Width <= iLabelWidth) Or (i = 0)
End If
Exit_Sub:
lbl.AutoSize = False
lbl.Width = iLabelWidth
Exit Sub
ErrorHandler:
' something went wrong ... put everything back
lbl.Caption = sText
Resume Exit_Sub
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.