Solved

Textbox length in characters?

Posted on 1998-06-08
3
384 Views
Last Modified: 2013-12-03
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
Comment
Question by:guadalupe
3 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1462846
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
 
LVL 1

Expert Comment

by:raygibbins
ID: 1462847
It would depend on the Characters - All characters can have different widths
0
 
LVL 1

Accepted Solution

by:
juliocoelho earned 50 total points
ID: 1462848
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now