Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Grid header width with word wrap
Message
From
12/11/2003 12:14:29
 
 
To
12/11/2003 11:22:25
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00849120
Message ID:
00849146
Views:
26
Michel,

This is not a simple task.

In the process that sets the header caption, you will need to adjust the column.Width property to fit the caption text.

To do this accurately, you will need to
1. Get the width of the text in pixels,
2. Determine whether the width of the column will accomodate the text. If not, adjust the column's width so the text will fit.

If you are using word wrap in your header (Column.WordWrap = .T.), it gets a lot more complicated since the column need only be wide enough to display the widest string that will result when the text is wrapped. This in turn is influenced by the height of your header and will change if the columnn Width property or grid HeaderHeight property is changed.

For a simple, one line, column header text display, here is a function that accurately returns the number of pixels needed to display the text. This may be used to determine your minimum column width. This function makes use of the form class .TextWidth() method. This approach is more accurate that using FONTMETRIC() with TXTWIDTH() which can return only an approximate pixel width. I think I got the idea for the function from Tamar Graynor, but I'm not sure. It has been a few years since I first wrote it.
********************************************************************************
FUNCTION TEXTWIDTH( toForm, toObject, tcText )
********************************************************************************
*
*   OVERVIEW:   Returns the width, in pixels, required to display a string in
*               the font characteristics specified for an object.
*         
*   PARAMETERS:
*
*      toForm   An object reference to the form containing toObject
*      toObject The object that will display tcText -- typically a label or header
*      tcText   The string for which to return the width in pixels.
*
*   RETURNS:    The number of pixels required to display tcText.
*
*   HISTORY:    Created 05/15/02 (JME)
*
*   COPYRIGHT:  2002, Jurix Data Corporation.
*
*   NOTE:       The function DOES NOT remove leading/trailing blanks. 
*
LOCAL ;
   llFontBold, lcFontName, llFontItalic, lnFontSize, ;
   llFormFontBold, lcFormFontName, llFormFontItalic, lnFormFontSize, ;
   lnWidth

*   Store the font characteristics of the object displaying the string.
WITH toObject

   llFontBold      = .FontBold
   llFontItalic    = .FontItalic
   lcFontName      = .FontName
   lnFontSize      = .FontSize

ENDWITH

*   Store the font characteristics of the form (to be restored below) and
*   replace the form font characteristics with object font characteristics
*   in preparation for calling the form .TextWidth() method.
WITH toForm

   llFormFontBold   = .FontBold
   llFormFontItalic = .FontItalic
   lcFormFontName   = .FontName
   lnFormFontSize   = .FontSize
   
   .FontBold   = llFontBold
   .FontItalic = llFontItalic
   .FontName   = lcFontName
   .FontSize   = lnFontSize
   
   *  The .TextWidth method of the form returns the number of pixels required
   *  to display the string in the current form font.
   lnWidth = .TextWidth( tcText )
   
   *   Restore the form's font characteristics.
   .FontBold   = llFormFontBold
   .FontItalic = llFormFontItalic
   .FontName   = lcFormFontName
   .FontSize   = lnFormFontSize
   
ENDWITH

IF VARTYPE( lnWidth ) # "N"
   lnWidth = 0
ENDIF
   
RETURN lnWidth      

ENDFUNC && TEXTWIDTH()
Call this method passing an object reference to the form, an object reference to the header and the caption string to be measured, i.e.:
lnWidthInPixels = TEXTWIDTH( thisform, thisform.myGrid.Columns[ 1 ].Controls[ 1 ], "My Header Caption" )
If you do not use word wrap in your headers, this should tell you the width your column needs to be to display the full string. I would use a 10 pixel (or greater) margin.

If you do use word wrap, then it gets more complex and requires a lot more code.

Let me know if I can provide more information.

Regards,
Jim Edgar
Jurix Data Corporation
jmedgar@yahoo.com

No trees were destroyed in sending this message. However, a large number of electrons were diverted from their ordinary activities and terribly inconvenienced.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform