Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Andy Kramek
  Where is Andy Kramek?
 Hot Springs Village
 Arkansas - United States
 Andy Kramek
 To: Russell Hill
  Where is Russell Hill?
 Sydney
 Australia
 Russell Hill
 Tags
Subject: RE: Design using Courier New
Thread ID: 209480 Message ID: 209805 # Views: 8 # Ratings: 0
Version: Visual FoxPro 9 Category: General VFP Topics
Date: Wednesday, December 31, 2008 11:54:33 AM         
   


> Hi all,
>
> I have an application where the chosen font is Courier New.
>
> As this is a fixed-width font, I should be able to design objects' widths more easily.
>
> But how do I relate a table's field size and the font size to the width of various objects?
>
> For example, let's say I have a field in MyTable called SURNAME, which is C(30).
>
> Is it possible to calculate the required width of a TextBox for various font sizes?

Yes: Here is some code that does precisely that and returns the exact length but also displays results in a modeless form. Call it using:
lnLen = CalcFont( "This Is my text String", "COURIER NEW", 10, "I" )


***********************************************************************
* Program....: CALCFONT.PRG
* Author.....: Andy Kramek
* Date.......: 28 August 2001
* Notice.....: Copyright (c) 2001 Tightline Computers Ltd, All Rights Reserved.
* Compiler...: Visual FoxPro 07.00.0000.9262 for Windows 
* Purpose....: Return the exact length of a string (in Pixels) in the specified font
* Prototype..: lnLen = CalcFont( "This String", "Arial", 9, "BI" )
***********************************************************************
LPARAMETERS tuInStr, tcFName, tnFSize, tcFStyle
LOCAL lcInStr, lnLen, lnParams, lnRetVal
lnParams = PCOUNT()
**********************************************************************
*** Set Default Values if nothing passed
*** Default style is "Arial", 9
**********************************************************************
IF lnParams < 4
  tcFStyle = ""
  IF lnParams < 3
    tnFSize = 9
    IF lnParams < 2
      tcFName = "Arial"
      IF lnParams < 1
        tuInStr = "1"
      ENDIF
    ENDIF
  ENDIF
ENDIF
**********************************************************************
*** Convert the input string to character equivalent
**********************************************************************
lcInStr = TRANSFORM( tuInStr )
lnLen = LEN( lcInStr )
**********************************************************************
*** Get the exact length in Pixels
**********************************************************************
lnRetVal = INT( TXTWIDTH( lcInStr, tcFName, tnFSize, tcFStyle) * ;
                FONTMETRIC(6, tcFName, tnFSize, tcFStyle))

**********************************************************************
*** Display full details in a simple form
**********************************************************************
loParams = CREATEOBJECT( 'line' )
WITH loParams
    .AddProperty( 'cString', lcInStr )
    .AddProperty( 'nChars', lnLen )
    .AddProperty( 'cFont', tcFName  )
    .AddProperty( 'cStyle', tcFStyle )
    .AddProperty( 'nSize', tnFSize )
    .AddProperty( 'nMax', lnLen * FONTMETRIC( 7, tcFName, tnFSize, tcFStyle ) )
    .AddProperty( 'nAvg', lnLen * FONTMETRIC( 6, tcFName, tnFSize, tcFStyle ) )
    .AddProperty( 'nExact', IIF(ISBLANK(lcInStr), "N/A", TRANSFORM( lnRetVal )) )
ENDWITH

**********************************************************************
*** This is a design time form, so we can use a Read Events to keep the
*** display modeless: Just comment this out if you don't want the form!!!
**********************************************************************
loDisp = CREATEOBJECT( 'DispFont', loParams )
READ EVENTS

**********************************************************************
*** And return the length
**********************************************************************
RETURN lnRetVal


**********************************************************************
*** Create a form to show the results here
**********************************************************************
DEFINE CLASS dispfont AS form

    Caption = "String Size Details"
    Name = "dispfont"
    Height = 92
    Width = 375
    Left = SYSMETRIC(1) - 400

    ADD OBJECT lblInString AS label WITH ;
        Caption = "Test String", ;
        Height = 17,Left = 13,Top = 8, Width = 61, ;
        TabIndex = 1, Name = "lblInString"

    ADD OBJECT txtinstring AS textbox WITH ;
        Height = 23,Left = 77, ReadOnly = .T., ;
        TabIndex = 2, Top = 5, Width = 293, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtInString"

    ADD OBJECT lblFont AS label WITH ;
        Caption = "Font Detail", ;
        Height = 17, Left = 13, Top = 36, Width = 61, ;
        TabIndex = 3, Name = "lblFont"

    ADD OBJECT txtfontname AS textbox WITH ;
        Height = 23, Left = 77, ReadOnly = .T., ;
        TabIndex = 4, Top = 33, Width = 145, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtFontName"

    ADD OBJECT txtfontsize AS textbox WITH ;
        Alignment = 3, Value = 0, Height = 23, Left = 230, ;
        ReadOnly = .T., TabIndex = 5, Top = 33, Width = 53, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtFontSize"

    ADD OBJECT txtfontstyl AS textbox WITH ;
        Height = 23, Left = 294, ReadOnly = .T., ;
        TabIndex = 6, Top = 33, Width = 75, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtFontStyl"

    ADD OBJECT lblChars AS label WITH ;
        Caption = "Characters", ;
        Height = 17, Left = 11,  Top = 64, Width = 63, ;
        TabIndex = 7, Name = "lblChars"

    ADD OBJECT txtnumchars AS textbox WITH ;
        Alignment = 3, Value = 0, Height = 23, Left = 77, ;
        ReadOnly = .T., TabIndex = 8, Top = 61, Width = 39, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtNumChars"

    ADD OBJECT lblPixels AS label WITH ;
        Caption = "Pixels: Max", Height = 17, Left = 119, ;
        Top = 64, Width = 62, TabIndex = 9, Name = "lblPixels"

    ADD OBJECT txtmaxlen AS textbox WITH ;
        Alignment = 3, Value = 0, Height = 23, Left = 183, ;
        ReadOnly = .T., TabIndex = 10, Top = 61, Width = 39, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtMaxLen"

    ADD OBJECT lblAvg AS label WITH ;
        Caption = "Avg", Height = 17, Left = 226, Top = 64, ;
        Width = 21, TabIndex = 11, Name = "lblAvg"

    ADD OBJECT txtAvgLen AS textbox WITH ;
        Alignment = 3, Value = 0, Height = 23, Left = 249, ;
        ReadOnly = .T., TabIndex = 12, Top = 61, Width = 39, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtAvgLen"

    ADD OBJECT lblAct AS label WITH ;
        Caption = "Exact", Height = 17, Left = 294, Top = 64, ;
        Width = 31, TabIndex = 13, Name = "lblAct"

    ADD OBJECT txtActLen AS textbox WITH ;
        Alignment = 3, Value = 0, Height = 23, Left = 330, ;
        ReadOnly = .T., TabIndex = 14, Top = 61, Width = 39, ;
        DisabledBackColor = RGB(192,192,192), ;
        DisabledForeColor = RGB(0,0,1), ;
        Name = "txtActLen"

    FUNCTION INIT( toParams )
        WITH toParams
           This.txtInString.Value = .cString
           This.txtNumChars.Value = .nChars
           This.txtFontName.Value = .cFont
           This.txtFontSize.Value = .nSize
           This.txtFontStyl.Value = .cStyle
           This.txtMaxLen.Value = .nMax
           This.txtAvgLen.Value = .nAvg
           This.txtActLen.Value = .nExact
           This.Show()
        ENDWITH
    ENDFUNC

    FUNCTION Unload
        CLEAR EVENTS
    ENDFUNC

ENDDEFINE



Regards
Andy Kramek
Microsoft MVP (Visual FoxPro)
Tightline Computers Inc, Akron Ohio, USA

ENTIRE THREAD

Design using Courier New Posted by Russell Hill @ 12/29/2008 8:55:58 AM
RE: Design using Courier New Posted by Samir Ibrahim @ 12/29/2008 9:47:24 AM
RE: Design using Courier New Posted by Russell Hill @ 12/29/2008 12:24:49 PM
RE: Design using Courier New Posted by Mike Yearwood @ 12/29/2008 7:58:23 PM
RE: Design using Courier New Posted by Samir Ibrahim @ 12/30/2008 10:32:13 AM
RE: Design using Courier New Posted by Stefan Wuebbe @ 12/30/2008 10:46:07 AM
RE: Design using Courier New Posted by Stefan Wuebbe @ 12/29/2008 9:49:08 AM
RE: Design using Courier New Posted by Russell Hill @ 12/29/2008 12:23:04 PM
RE: Design using Courier New Posted by Stefan Wuebbe @ 12/29/2008 5:49:35 PM
RE: Design using Courier New Posted by Russell Hill @ 12/31/2008 1:54:57 AM
RE: Design using Courier New Posted by Gerrit Masee @ 12/29/2008 10:33:40 AM
RE: Design using Courier New Posted by Russell Hill @ 12/29/2008 12:29:21 PM
RE: Design using Courier New Posted by Cetin Basoz @ 12/29/2008 12:23:39 PM
RE: Design using Courier New Posted by Russell Hill @ 12/29/2008 12:27:04 PM
RE: Design using Courier New Posted by tushar @ 12/29/2008 1:18:59 PM
RE: Design using Courier New Posted by Yousfi Benameur @ 12/29/2008 5:12:38 PM
RE: Design using Courier New Posted by Russell Hill @ 12/30/2008 6:11:13 AM
RE: Design using Courier New Posted by Yousfi Benameur @ 12/30/2008 2:41:53 PM
RE: Design using Courier New Posted by Russell Hill @ 12/31/2008 1:54:13 AM
RE: Design using Courier New Posted by Yousfi Benameur @ 12/31/2008 10:16:43 AM
RE: Design using Courier New Posted by Yousfi Benameur @ 12/30/2008 12:14:50 AM
RE: Design using Courier New Posted by Russell Hill @ 12/30/2008 4:12:26 AM
RE: Design using Courier New Posted by Andy Kramek @ 12/31/2008 11:54:33 AM
RE: Design using Courier New Posted by Yousfi Benameur @ 12/31/2008 12:33:17 PM