By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

Change Textbox Width

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Steve" <no****@nospam.spam> wrote in message
news:lG****************@newsread2.news.atl.earthli nk.net
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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
Nov 13 '05 #2

P: n/a
Thanks, Dirk!

Yes, that's the name of Stephen's project.

Steve
"Dirk Goldgar" <dg@NOdataSPAMgnostics.com> wrote in message
news:ua*************@TK2MSFTNGP15.phx.gbl...
"Steve" <no****@nospam.spam> wrote in message
news:lG****************@newsread2.news.atl.earthli nk.net
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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Nov 13 '05 #3

P: n/a
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

Nov 13 '05 #4

P: n/a
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.
<ji********@compumarc.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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


Nov 13 '05 #5

P: n/a
Stephen Lebans wrote:
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

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.