Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP 6.0 - Grid - Column Width

Status
Not open for further replies.

DChalom

Programmer
Jun 8, 2002
59
US
Does someone have a way, a class i can use to dynamicly set the column width to either the longest data in the column or the length of the header, which ever is longer?

You were all so helpful last time. I look forward to your input for this....

Dorian C. Chalom
 
Dorian,

This is not the answer you want to hear, but the easiest approach might be to upgrade to VFP 8.0, which has an Autofit method that does exactly what you want.

In 6.0, it should be possible to do what you want, but very difficult, because the column width is expressed in pixels but the width of the contents of the cells or headers is measured in characters.

I hope you manange to find a solution.

Mike



Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Unfortunarely that is not an option, and i hope some one has a solution to this issue....

Dorian C. Chalom
 
Hi Dorian.

I must say that I agree with Mike Lewis. Why work so hard to write functionality that is native in version 8?

Having said that, put this code in some method of your grid header (I suppose dblClick() is the logical choice) to call the custom SetColWidth() method that you will add to your grid class. This code is untested, but it should give you some idea of how to proceed.

Coder in the header's dblClick()

Code:
THIS.Parent.Parent.SetColWidth( This.Parent )

This code in SetColWidth() method

Code:
LPARAMETERS toColumn
IF NOT EMPTY( toColumn.ControlSource )
  lnNewWidth = LEN( EVAL( toColumn.ControlSource ) )
  liFactor = THIS.GetFontmetric( toColumn )
  lnNewwidth = lnNewwidth * liFactor 
  toColumn.Width = lnNewWidth
ENDIF

The code for the GetFontMetric method is straight from the source code for the builders that ships with VFP.

Code:
PARAMETER oref

IF TYPE("oref") <> "O"
  RETURN 0
ENDIF

IF TYPE("m.oref.Fontname") <> "U"
  m.fname = m.oref.fontname
ELSE
  m.fname = ""
ENDIF

IF TYPE("m.oref.Fontsize") <> "U"
  m.fsize = m.oref.fontsize
ELSE
  m.fsize = ""
ENDIF

m.fStyle = ""
IF TYPE("m.oref.FontBold") <> "U" AND m.oref.FontBold
  m.fStyle = m.fStyle + "B"
ENDIF
IF TYPE("m.oref.FontItalic") <> "U" AND m.oref.FontItalic
  m.fStyle = m.fStyle + "I"
ENDIF
IF TYPE("m.oref.FontOutline") <> "U" AND m.oref.FontOutline
  m.fStyle = m.fStyle + "O"
ENDIF
IF TYPE("m.oref.FontShadow") <> "U" AND m.oref.FontShadow
  m.fStyle = m.fStyle + "S"
ENDIF
IF TYPE("m.oref.FontShadow") <> "U" AND .oref.FontUnderline
  m.fStyle = m.fStyle + "U"
ENDIF

m.ffactor = IIF(EMPTY(m.fname), THIS.BldrMetric, FONTMETRIC(6,m.fname,m.fsize,m.fstyle))
	
RETURN m.ffactor


Marcia G. Akins
 
That would be great except the line
lnNewWidth = LEN( EVAL( toColumn.ControlSource ) )

does not take into account the different field types. i.e., can't take a Len() of a numeric field

Dorian C. Chalom
 
All that formula does is use the FontMetric function which i already use. And fro some reason mu numberic fields are still way off

Dorian C. Chalom
 
Hello Dorian.

As I said, the code was untested an off the top of my head. I posted it just to help you get started. At the risk of repeating myself, I personally think it is a waste of time trying to re-invent the wheel to reporduce functionality that is native in the product in version 8.

Code:
lnNewWidth = LEN( TRANSFORM( EVAL( toColumn.ControlSource ) ) )

Good luck with your pursuit.



Marcia G. Akins
 
Here I got it to work the way I wanted it to...it must of been the order of the functions

Here are the two function calls:
thisform.TextWidth(EVAL(ALLTRIM(cField)))
ALLTRIM(TRANSFORM(EVAL(cField)))

And here is the final reslut code:
LPARAMETER tCol
cField = SUBSTR(tCol.ControlSource, AT(".", tCol.ControlSource) + 1)
cFile = LEFT(tCol.ControlSource, AT(".", tCol.ControlSource) - 1)


SELECT MAX(thisform.TextWidth(ALLTRIM(TRANSFORM(EVAL(cField))))) AS x ;
FROM (cFile) ;
INTO CURSOR cMax

lCol = cMax.x + 1
cHdr = tCol.Header1.Caption
lHdr = thisform.TextWidth(ALLTRIM(cHdr)) + 1

RETURN IIF(lCol > lHdr, lCol, lHdr)

Dorian C. Chalom
 
Here is the final code i ended up using:

LPARAMETER tCol
cField = SUBSTR(tCol.ControlSource, AT(".", tCol.ControlSource) + 1)
cFile = LEFT(tCol.ControlSource, AT(".", tCol.ControlSource) - 1)


SELECT MAX(LEN(ALLTRIM(TRANSFORM(&cField)))) AS x ;
FROM (cFile) ;
INTO CURSOR cMax

lnLen = MAX(cMax.x, LEN(tCol.Header1.Caption))
USE IN SELECT("cMax")
RETURN thisform.TextWidth(REPLICATE("W", lnLen)) * 0.8

Dorian C. Chalom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top