Solved

Textbox length in characters?

Posted on 1998-06-08
3
382 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

759 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

19 Experts available now in Live!

Get 1:1 Help Now