Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting the ColumnWidths propery programatically
Message
From
30/12/2002 13:22:07
 
 
To
12/12/2002 17:39:26
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00732471
Message ID:
00736650
Views:
7
Hi Robert,

One potential problem with your code is that "MAX" is a reserved work in FoxPro, so while the interpreter may not get confused when you compile and run the code, there is a good change that it will throw a syntax error.

A second is that the ColumnWidths property of a combobox takes a string.

Lastly the ColumnWidths property is stated in pixels (or foxels, if anyone still uses foxels), but your code returns the character length, not the pixel length.

You can translate character length to pixels using a complicated combination of FONTMETRIC() and TXTWIDTH(), but since your combobox is undoubtedly on a form, there is a faster and less complex form method to get the number of pixels required to display the text using the form .TextWidth() method.

The TEXTWIDTH() function below is a wrapper for the form .TextWidth() method that returns the number of pixels required to display a string in the font characteristics of the object reference passed as toObject.

Try this code
LOCAL lnLen, lnMax, lcString

lnMax = 0
lcString = ""

SELECT domenii

SCAN

    IF lnMax < LEN(ALLTRIM(domenii.denumire))
    
		lnMax 		= LEN(ALLTRIM(domenii.denumire))
        lcString 	= ALLTRIM(domenii.denumire)
        
    ENDIF
    
ENDSCAN

*    Translate character length to pixel length
lnLen = TEXTWIDTH( thisform, this, lcString )

this.Columnwidths = TRANSFORM( lnLen )


*********************************************************************************
FUNCTION TEXTWIDTH( toForm, toObject, tcText )
********************************************************************************
*
*	OVERVIEW:   Returns the width, in pixels required to display a string in
*		    the font specified for an object.
*			
*	PARAMETERS:
*		toForm		An object reference to the form containing toObject
*		toObject	The object that will display tcText
*		tcText		The text to be displayed
*
*	RETURNS:	The number of pixels required to display tcText
*
LOCAL ;
	llFontBold, lcFontName, llFontItalic, lnFontSize, ;
	llFormFontBold, lcFormFontName, llFormFontItalic, lnFormFontSize, ;
	lnWidth

WITH toObject
	llFontBold	= .FontBold
	llFontItalic	= .FontItalic
	lcFontName	= .FontName
	lnFontSize	= .FontSize
ENDWITH

WITH toForm

	llFormFontBold	= .FontBold
	llFormFontItalic= .FontItalic
	lcFormFontName	= .FontName
	lnFormFontSize	= .FontSize
	
	.FontBold 	= llFontBold
	.FontItalic	= llFontItalic
	.FontName 	= lcFontName
	.FontSize 	= lnFontSize
	
	lnWidth 	= .TextWidth( tcText )
	
	.FontBold 	= llFormFontBold
	.FontItalic	= llFormFontItalic
	.FontName 	= lcFormFontName
	.FontSize 	= lnFormFontSize
	
ENDWITH

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

ENDFUNC && TEXTWIDTH()
Good luck,
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
Reply
Map
View

Click here to load this message in the networking platform