473,651 Members | 3,068 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 14485
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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**@packairin c.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_Fo rmat(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 .AssignmentComp any)

'MsgBox lChangeLength
Me!AssignmentPr oject.LeftMargi n = 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**@packairin c.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.publ ic.com> wrote in
message news:44******@2 12.67.96.135...
Try something like this (Access 2003) in a format event of a report

Private Sub GroupHeader0_Fo rmat(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 .AssignmentComp any)

'MsgBox lChangeLength
Me!AssignmentPr oject.LeftMargi n = 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**@packairin c.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******** ***********@new ssvr11.news.pro digy.com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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 TextHeightWidth Ver45.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**@packairin c.com> wrote in message
news:va******** *************** *******@athenet .net...

"Randy Harris" <pl****@send.no .spam> wrote in message
news:lg******** ***********@new ssvr11.news.pro digy.com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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 TextHeightWidth Ver45.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.co m>
wrote in message news:dG******** ***********@urs a-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**@packairin c.com> wrote in message
news:va******** *************** *******@athenet .net...

"Randy Harris" <pl****@send.no .spam> wrote in message
news:lg******** ***********@new ssvr11.news.pro digy.com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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 TextHeightWidth Ver45.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_EXTERNALLEAD ING Or DT_NOCLIP Or DT_SINGLELINE)

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

"Randy Harris" <pl****@send.no .spam> wrote in message
news:lg******** ***********@new ssvr11.news.pro digy.com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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 TextHeightWidth Ver45.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**@packairin c.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_EXTERNALLEAD ING Or DT_NOCLIP Or DT_SINGLELINE)

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

"Randy Harris" <pl****@send.no .spam> wrote in message
news:lg******** ***********@new ssvr11.news.pro digy.com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairin c.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 TextHeightWidth Ver45.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

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

Similar topics

5
11564
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 correct once the data is in Access (or during the import process itself). Furthermore, the text files are poorly set up, such that some records may be "shifted" over a few characters, and therefore the fixed width nature of the file is corrupted. ...
0
1282
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 the large text file, then loop through the arraylist of column headers, doing some math to grab the substrings - but this is going to be a sluggish way to do it. I am hoping others some tricks for doing something like this? Thanks!
1
3764
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 file, import the text file as fixed width text , and then run an update query to copy the appropriate info into fields of a different table. Is it possible to write a macro to do all these steps? Also, is it
1
3168
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 extract data from text files and import it into datatable. The format of the text file is fixed length/width. Eg. http://img174.imageshack.us/img174/8457/untitledpp4.jpg I am having trouble to do so as I am not sure how to start. I had done...
0
983
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 object at a specified position (like X=50; Y=50). But this position may be top_right (or bottom_right or top_left or ...) of HtmlGenericControl object.
3
1511
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
3983
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 row in a table etc... But does fixed-with mean that every column has a fixed with: for example first column is 10 char wide and second column is 30 char wide?
4
7036
by: pasu | last post by:
how to calculate two text box values and display it in another text box dynamically??
0
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.