473,503 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculate Width of text

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.
May 17 '06 #1
12 14440
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.


May 17 '06 #2
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
May 17 '06 #3
Try something like this (Access 2003) in a format event of a report

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)

Dim lChangeLength As Long

With Me
.FontBold = True
.FontItalic = True
.FontSize = 12
.FontName = "Times New Roman"
End With

lChangeLength = Me.TextWidth(Me.AssignmentCompany)

'MsgBox lChangeLength
Me!AssignmentProject.LeftMargin = lChangeLength + 100

End Sub
This sets the relevant font for measuring the width of the field and
TextWidth returns the value in the scale set for the report.

The final line actually uses the value to display an offset for another
field so that the two fields appear in different formats at a set distance
apart no matter what the data being displayed is.
--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"paii, Ron" <pa**@packairinc.com> wrote in message
news:NJ******************************@athenet.net. ..
Sorry about that last one.

Does anyone know how to calculate the width a string of text for given
Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the
left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.

May 17 '06 #4
Of course I may have answered a different question. Ooops! (g)

This only applies to reports.

--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"Craig Alexander Morrison" <ca*@microsoft.newsgroups.public.com> wrote in
message news:44******@212.67.96.135...
Try something like this (Access 2003) in a format event of a report

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)

Dim lChangeLength As Long

With Me
.FontBold = True
.FontItalic = True
.FontSize = 12
.FontName = "Times New Roman"
End With

lChangeLength = Me.TextWidth(Me.AssignmentCompany)

'MsgBox lChangeLength
Me!AssignmentProject.LeftMargin = lChangeLength + 100

End Sub
This sets the relevant font for measuring the width of the field and
TextWidth returns the value in the scale set for the report.

The final line actually uses the value to display an offset for another
field so that the two fields appear in different formats at a set distance
apart no matter what the data being displayed is.
--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"paii, Ron" <pa**@packairinc.com> wrote in message
news:NJ******************************@athenet.net. ..
Sorry about that last one.

Does anyone know how to calculate the width a string of text for given
Font
name and size?
I want to buildup a block of text strings to display in a unbound
control,
that has limited width but multiple lines. Currently I am using the
left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.



May 17 '06 #5

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font name and size?
I want to buildup a block of text strings to display in a unbound control, that has limited width but multiple lines. Currently I am using the left$() function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of the control. To keep the output nice, I don't want to use a fixed pitch
font.


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function
May 17 '06 #6
Are you using English Text?

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
> wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its
> cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given Font >> name and size?
>> I want to buildup a block of text strings to display in a unbound control, >> that has limited width but multiple lines. Currently I am using the left$() >> function to trim the text to a standard number of characters. I want a
>> function to return the number of characters that will fit in the width of >> the control. To keep the output nice, I don't want to use a fixed
>> pitch
>> font.
>>
>


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function

May 17 '06 #7
Yes,

The text is normally a persons name followed by a ":" and then some
information.

ex. I would pass the function text one line at a time adding the result to
the control

..21
Doe, John: 8 Hours Vacation
Walacs, Henry: 8 Hours Vacation
Clar, Sue: 8 Hours Vacation

May come back like this

..21
Doe, John: 8 Hours Vacation
Walacs, Henry: 8 Hours Vacation
Clar, Sue: 8 Hours Vacation

It looks like longer strings return fewer Twips.

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:dG*******************@ursa-nb00s0.nbnet.nb.ca...
Are you using English Text?

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
> wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its > cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given
Font
>> name and size?
>> I want to buildup a block of text strings to display in a unbound

control,
>> that has limited width but multiple lines. Currently I am using the

left$()
>> function to trim the text to a standard number of characters. I want
a >> function to return the number of characters that will fit in the

width of
>> the control. To keep the output nice, I don't want to use a fixed
>> pitch
>> font.
>>
>

I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function GetLeftQty() to calculate the number of characters that will fit into the control.
fTextWidth() sometimes underestimates the TWIPS causing my character count to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", " & ctlr.Width & ", " & Left$(strTxt, iLen)
End Function


May 17 '06 #8
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.
> Sorry about that last one.
>
> Does anyone know how to calculate the width a string of text for given
Font
> name and size?
> I want to buildup a block of text strings to display in a unbound control,> that has limited width but multiple lines. Currently I am using the left$()> function to trim the text to a standard number of characters. I want
a> function to return the number of characters that will fit in the width
of> the control. To keep the output nice, I don't want to use a fixed
pitch> font.
>


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ",

" & ctlr.Width & ", " & Left$(strTxt, iLen)
End Function

May 18 '06 #9
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the supplied code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com> > wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its > cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given
Font
>> name and size?
>> I want to buildup a block of text strings to display in a unbound control,
>> that has limited width but multiple lines. Currently I am using the

left$()
>> function to trim the text to a standard number of characters. I
want a >> function to return the number of characters that will fit in the width
of
>> the control. To keep the output nice, I don't want to use a fixed
pitch >> font.
>>
>

I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function GetLeftQty() to calculate the number of characters that will fit into the control.
fTextWidth() sometimes underestimates the TWIPS causing my character count to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) &

", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function


May 18 '06 #10
The solution is not to change an intrinsic constant. You are now tied to the
current resolution setting of your graphics card.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:Ga******************************@athenet.net. ..
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the

supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..
>
> "Randy Harris" <pl****@send.no.spam> wrote in message
> news:lg*******************@newssvr11.news.prodigy. com...
> > * Tom van Stiphout:
> > > On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com> > > > wrote:
> > >
> > > There is no easy way to do this. The Windows API GetTextExtent and its > > > cousins will give you some information, but this is not a trivial
> > > route to take.
> > >
> > > -Tom.
> > >
> > >
> > >> Sorry about that last one.
> > >>
> > >> Does anyone know how to calculate the width a string of text for

given
> Font
> > >> name and size?
> > >> I want to buildup a block of text strings to display in a unbound
> control,
> > >> that has limited width but multiple lines. Currently I am using
> > >> the
> left$()
> > >> function to trim the text to a standard number of characters. I want
a
> > >> function to return the number of characters that will fit in the

width
> of
> > >> the control. To keep the output nice, I don't want to use a fixed

pitch
> > >> font.
> > >>
> > >
> >
> > I didn't have the time to go check it out, but I think Stephen Lebans
> > has a solution for this.
> >
> > --
> > Randy Harris
> > tech at promail dot com
> > I'm pretty sure I know everything that I can remember.
>
> I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function > GetLeftQty() to calculate the number of characters that will fit into the > control.
> fTextWidth() sometimes underestimates the TWIPS causing my character count > to be high. and other times overestimates the TWIPS causing my
> character
> count to be low.
>
> Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
> Integer) As Integer
> ' Function returns the number of character of strTxt that will fit in
> 1
> line of control ctlr
>
> Dim iLen As Integer ' Text length
> Dim lngTextWidth As Long
> Dim ctlW As Long
>
> ctlW = ctlr.Width
> iLen = Len(strTxt)
> If lmax > 0 Then
> iLen = MinV(iLen, lmax)
> End If
>
> Do While iLen > 1
> lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))
>
> ' Does text fit? If yes then exit
> If lngTextWidth < ctlW Then Exit Do
>
> ' Decrease text length
> iLen = iLen - 1
> Loop
> If iLen > 2 Then
> iLen = iLen - 2
> End If
> GetLeftQty = iLen
> 'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) &

",
"
> & ctlr.Width & ", " & Left$(strTxt, iLen)
> End Function
>
>



May 18 '06 #11
You use WidthTwips = .Right * (TWIPSPERINCH / lngDPI) to calculate width. Is
GetDeviceCaps returning the wrong value? If so, what problems would I have
if lngDPI is adjusted for this calculation?

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:Q1*******************@ursa-nb00s0.nbnet.nb.ca...
The solution is not to change an intrinsic constant. You are now tied to the current resolution setting of your graphics card.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:Ga******************************@athenet.net. ..
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the

supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..
>
> "Randy Harris" <pl****@send.no.spam> wrote in message
> news:lg*******************@newssvr11.news.prodigy. com...
> > * Tom van Stiphout:
> > > On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron"

<pa**@packairinc.com>
> > > wrote:
> > >
> > > There is no easy way to do this. The Windows API GetTextExtent and
its
> > > cousins will give you some information, but this is not a trivial
> > > route to take.
> > >
> > > -Tom.
> > >
> > >
> > >> Sorry about that last one.
> > >>
> > >> Does anyone know how to calculate the width a string of text for
given
> Font
> > >> name and size?
> > >> I want to buildup a block of text strings to display in a
unbound > control,
> > >> that has limited width but multiple lines. Currently I am using
> > >> the
> left$()
> > >> function to trim the text to a standard number of characters. I

want
a
> > >> function to return the number of characters that will fit in the
width
> of
> > >> the control. To keep the output nice, I don't want to use a fixed pitch
> > >> font.
> > >>
> > >
> >
> > I didn't have the time to go check it out, but I think Stephen Lebans > > has a solution for this.
> >
> > --
> > Randy Harris
> > tech at promail dot com
> > I'm pretty sure I know everything that I can remember.
>
> I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function
> GetLeftQty() to calculate the number of characters that will fit into

the
> control.
> fTextWidth() sometimes underestimates the TWIPS causing my character

count
> to be high. and other times overestimates the TWIPS causing my
> character
> count to be low.
>
> Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
> Integer) As Integer
> ' Function returns the number of character of strTxt that will fit in > 1
> line of control ctlr
>
> Dim iLen As Integer ' Text length
> Dim lngTextWidth As Long
> Dim ctlW As Long
>
> ctlW = ctlr.Width
> iLen = Len(strTxt)
> If lmax > 0 Then
> iLen = MinV(iLen, lmax)
> End If
>
> Do While iLen > 1
> lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))
>
> ' Does text fit? If yes then exit
> If lngTextWidth < ctlW Then Exit Do
>
> ' Decrease text length
> iLen = iLen - 1
> Loop
> If iLen > 2 Then
> iLen = iLen - 2
> End If
> GetLeftQty = iLen
> 'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen))

& ",
"
> & ctlr.Width & ", " & Left$(strTxt, iLen)
> End Function
>
>



May 18 '06 #12

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:Q1*******************@ursa-nb00s0.nbnet.nb.ca...
The solution is not to change an intrinsic constant. You are now tied to the current resolution setting of your graphics card.

--

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


You are right!

I modifyed GetLeftQty() to calculate the number of twips for the character
"X" and used that to reduce the number width of the control in the loop

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
On Error Resume Next
' Function returns the number of character of strTxt that will fit in 1 line
of control ctlr

Dim iLen As Integer ' Text length
Dim lWidth As Long ' Adjusted control width

iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

lWidth = ctlr.Width - fTextWidth(ctlr, "X") ' Reduce control width by 1
character.
If lWidth < 1 Then
GetLeftQty = 1 ' Return 1 if adjusted control width is < 1 Twip
Exit Function
End If

Do While iLen > 1

' Does text fit? If yes then exit
If fTextWidth(ctlr, Left$(strTxt, iLen)) < lWidth Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
GetLeftQty = iLen
End Function
May 18 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11542
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can...
0
1262
by: Masa Ito | last post by:
I have large fixed width text files that I need to parse. I regex'd a file that defines the columns - so I now have a large arraylist of column headers and column widths. I can read a line of...
1
3754
by: ghadley_00 | last post by:
Hi, I have a MS access database table for which I regularly need to import fixed width text data. At present I have to to cut and paste the text data from its source to a text file, save the...
1
3163
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
0
967
by: papillonhn | last post by:
Please help me. In my HtmlGenericControl object has: an image and a string. Is there anyway to determine or calculate the width of HtmlGenericControl. I need to display this HtmlGenericControl...
3
1493
by: bottomupthinker | last post by:
Hi all i have a string cotaining numeric values also. the font is courier New now i want to calculate its width please send the code
4
3953
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
4
7029
by: pasu | last post by:
how to calculate two text box values and display it in another text box dynamically??
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.