Change Textbox Width

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I need a way to programatically change the width of a textbox to display all
the data knowing the font, font size and number of characters. Does any oone
know of a reference somewhere ot a way to determine the width in twips for
say Times New Roman 12, 35 characters or say Arial 11, 26 characters?

Thanks,

Steve
 
Steve said:
I need a way to programatically change the width of a textbox to
display all the data knowing the font, font size and number of
characters. Does any oone know of a reference somewhere ot a way to
determine the width in twips for say Times New Roman 12, 35
characters or say Arial 11, 26 characters?

Thanks,

Steve

IIRC, Stephen Lebans has code to do this on his site at

www.lebans.com

Maybe the project is called "AutoSizeTextBox"; I'm not sure.
 
Just in case Stephen's code doesn't do what you want:

On http://www.adobe.com you should be able to find:

Adobe Font Metric File Format Specification (#5004)

If you have the .afm file, it contains the information you need to
determine precisely how much space the characters in a font will use.
I usually ignore the kerning information since my uses don't require
that much accuracy. The following file (zipped A97 format) contains
character size information (no kerning info) for the 14 standard fonts
used for pdf documents:

http://www.oakland.edu/~fortune/FontMetrics.zip

To get the width the characters will take I use:

Public Function GetFontWidth(strIn As String, strFontName As String,
dblFontSize As Double) As Double
Dim lngTemp As Long
Dim MetricRS As Recordset
Dim MyDB As Database
Dim strSQL As String
Dim intI As Integer
Dim strChar As String
Dim lngASC As Long

GetFontWidth = 0#
If Len(strIn) = 0 Then Exit Function
lngTemp = 0
Set MyDB = CurrentDb
strSQL = "SELECT * FROM tbl" & strFontName & "Metrics;"
Set MetricRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
For intI = 1 To Len(strIn)
strChar = Mid(strIn, intI, 1)
lngASC = Asc(strChar)
MetricRS.FindFirst "[ASCIINumber] = " & CStr(lngASC)
If Not MetricRS.NoMatch Then
lngTemp = lngTemp + MetricRS("Width")
End If
Next intI
MetricRS.Close
Set MetricRS = Nothing
Set MyDB = Nothing
GetFontWidth = lngTemp * dblFontSize / 1000#
End Function

The fonts you mention are TrueType. More than you'll ever want to know
about TrueType fonts can be found at:

http://www.truetype.demon.co.uk/index.htm

Note that different characters produce different widths in proportional
fonts (most fonts). Courier is an example of a fixed-width font.
There are 20 twips to a PostScript point. There are 72 PostScript
points to an inch.

James A. Fortune

The default unit size (1/72 inch) is approximately the same as a
"point," a unit widely used in the printing industry. It is not
exactly the same as a point, however; there is no universal definition
of a point.
-- Postscript Language Reference Manual, Third Edition, Adobe Systems
Incorporated
 
The code on my site works for any font. Only one line of code is
required to return a value in TWIPS which can be directly applied to the
Text/Label control to perform autosizing. Further it is a multiline
aware solution.

http://www.lebans.com/textwidth-height.htm
TextHeightWidth.zip is a replacement for the Report object's TextWidth
and TextHeight methods. It is multiline aware and can work in both
Report and Form views. Includes a sample report to show you how to
autosize individual controls with different formatting on the same line
to simulate RTF style text.


History

Version 4.5: Increased accuracy of auto sizing for a ListBox.

Version 4.3: Added sample Form to demonstrate auto sizing for a ListBox.

Version 4.2: Added report to demonstrate auto sizing to mix text
formatting on one row.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Just in case Stephen's code doesn't do what you want:

On http://www.adobe.com you should be able to find:

Adobe Font Metric File Format Specification (#5004)

If you have the .afm file, it contains the information you need to
determine precisely how much space the characters in a font will use.
I usually ignore the kerning information since my uses don't require
that much accuracy. The following file (zipped A97 format) contains
character size information (no kerning info) for the 14 standard fonts
used for pdf documents:

http://www.oakland.edu/~fortune/FontMetrics.zip

To get the width the characters will take I use:

Public Function GetFontWidth(strIn As String, strFontName As String,
dblFontSize As Double) As Double
Dim lngTemp As Long
Dim MetricRS As Recordset
Dim MyDB As Database
Dim strSQL As String
Dim intI As Integer
Dim strChar As String
Dim lngASC As Long

GetFontWidth = 0#
If Len(strIn) = 0 Then Exit Function
lngTemp = 0
Set MyDB = CurrentDb
strSQL = "SELECT * FROM tbl" & strFontName & "Metrics;"
Set MetricRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
For intI = 1 To Len(strIn)
strChar = Mid(strIn, intI, 1)
lngASC = Asc(strChar)
MetricRS.FindFirst "[ASCIINumber] = " & CStr(lngASC)
If Not MetricRS.NoMatch Then
lngTemp = lngTemp + MetricRS("Width")
End If
Next intI
MetricRS.Close
Set MetricRS = Nothing
Set MyDB = Nothing
GetFontWidth = lngTemp * dblFontSize / 1000#
End Function

The fonts you mention are TrueType. More than you'll ever want to know
about TrueType fonts can be found at:

http://www.truetype.demon.co.uk/index.htm

Note that different characters produce different widths in proportional
fonts (most fonts). Courier is an example of a fixed-width font.
There are 20 twips to a PostScript point. There are 72 PostScript
points to an inch.

James A. Fortune

The default unit size (1/72 inch) is approximately the same as a
"point," a unit widely used in the printing industry. It is not
exactly the same as a point, however; there is no universal definition
of a point.
-- Postscript Language Reference Manual, Third Edition, Adobe Systems
Incorporated
 
Stephen said:
The code on my site works for any font. Only one line of code is
required to return a value in TWIPS which can be directly applied to the
Text/Label control to perform autosizing. Further it is a multiline
aware solution.

http://www.lebans.com/textwidth-height.htm
TextHeightWidth.zip is a replacement for the Report object's TextWidth
and TextHeight methods. It is multiline aware and can work in both
Report and Form views. Includes a sample report to show you how to
autosize individual controls with different formatting on the same line
to simulate RTF style text.


History

Version 4.5: Increased accuracy of auto sizing for a ListBox.

Version 4.3: Added sample Form to demonstrate auto sizing for a ListBox.

Version 4.2: Added report to demonstrate auto sizing to mix text
formatting on one row.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.

Stephen,

I didn't mean to imply that your code contributions for text boxes and
RTF won't work. Those are great features BTW.

James A. Fortune
 
Back
Top