Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scrolling label captions

Status
Not open for further replies.

ggdev

Programmer
May 29, 2007
12
GB
I have an application with variable captions. I would like to detect the length of the caption and if it exceeds the width of the field I would like to make it scroll.

The problem I have is that I cannot check the number of characters because of the variable width of different characters.

Does anyone know of a means to determine the pixel width of a set of characters so it can be compared to the pixel width of a label caption?

Thank you.
 

Can you use the Len() function?

Let's say you have a Label on the Frame, set the AutoSize property of Label to True:
Code:
If Len(Label1.Caption) > Frame1.Width Then
   [blue]...scralll your text[/blue]
End If

Have fun.

---- Andy
 
There is a function on each form called TextWidth. This will return the width of the supplied text in whatever font and scale mode you have chosen.

Ex:

Code:
Option Explicit

Private Sub Form_Load()
    
    Me.ScaleMode = vbPixels
    MsgBox Me.TextWidth("Hello World")
    MsgBox Me.TextWidth("W")
    MsgBox Me.TextWidth("i")
    
    
End Sub

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you very much for both suggestions.
I have found that the ScaleMode & Me.TextWidth suggestion worked a treat and enabled me to achieve exactly what I needed to do.
Thanks very much.
 
For some interesting issues with TextWidth, see thread222-1407322 down at the bottom. I'm still stumped; if you change the font in a label and try to get the width, you'll still get the width in terms of the form's font, not the label's.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top