• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Textbox length in characters?

In VB5, how can you get an integer which represents the length, in chracters, of a textbox even if it is empty.
The  widthproperty is in twips (which I have been roughly translating to 1/100 + 3 to get characters, but its
rough) and the TextLength Function is only for the lenght of  actual text (written characters).
0
guadalupe
Asked:
guadalupe
1 Solution
 
alamoCommented:
This is a duplicate question (you may have submitted it twice by mistake) - you should choose "delete" to delete this question and reclaim the points before someone tries to answer it!
0
 
raygibbinsCommented:
It would depend on the Characters - All characters can have different widths
0
 
juliocoelhoCommented:
Try this:


Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long

Private Type TEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
    End Type

Public Function AverageChar(TestObj As Object, ReturnMode As Integer) As Long
'returns average character length in pixels (0) or twips (1)
Dim foo As Long, Tm As TEXTMETRIC

foo = GetTextMetrics(TestObj.hdc, Tm)
If ReturnMode = 1 Then
   AverageChar = Tm.tmAveCharWidth * Screen.TwipsPerPixelX
Else
   AverageChar = Tm.tmAveCharWidth
End If

End Function


With kind regards.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now