Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to determine TextBox width?
Message
From
21/09/2009 07:04:25
 
 
To
21/09/2009 01:55:16
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01425392
Message ID:
01425408
Views:
62
>>Hi,
>>
>>I have a TextBox control which is bound to a C32 field.
>>
>>How do I programatically find out the shortest width of the TextBox and make sure any 32 characters inputed will be displayed fully in the textbox?
>>
>>I was thinking:
>>TextBox.Width = 32 * FONTMETRIC(7, TextBox.FontName, TextBox.FontSize)
>>
>>But FONTMETRIC(7) is crazy... for Arial font size 10, it gives me 35 about 6 time of FONTMETRIC(6)...
>
>Hi
>Try
>
>WITH YourTextbox
>  lcFontStyle = IIF(.FONTBOLD,"B","") + ;
>   IIF(.FONTITALIC,"I","") + ;
>   IIF(.FONTSTRIKETHRU,"-","") + ;
>   IIF(.FONTUNDERLINE,"U","")
>
>  lnFontMetric = FONTMETRIC(6,.FONTNAME ,.FONTSIZE,lcFontStyle)
>
>.WIDTH = CEILING(TXTWIDTH(.VALUE, ;
>  .FONTNAME,.FONTSIZE,lcFontStyle)+1)*lnFontMetric
>
>ENDWITH &&YourTextbox
>
>
>Sorry for beeing a bit short in time. If you search my old messages, you will find some code that will calculate the hight as well.
>
>Agnes

It is possible - untested though - that form.TextWidth() and form.TextHeight() give a better result than the average fontsize. The only drawback is that you have to set the form's font attributes to match those of the textbox


Update: something like this
for 'i' it returns 3
for 'WWWWWWWWWW' it returns 110
for 'WWWWWWWWWWi' it returns 113

call:

local obj, nWidth, nHeight
obj = m.thisform.Text1
if( Text_Properties_Get(m.obj, rtrim(m.obj.Text), @m.nWidth, @m.nHeight) )
   ?m.nWidth, m.nHeight
endif
*---

function Text_Properties_Get(obj, txt, Width, Height)

	local Success
	Success = TRUE

	
	local FontBold, FontItalic, FontName, FontSize, FontStrikeThru, FontUnderLine
	
	with _screen
		FontBold		= .FontBold
		FontItalic		= .FontItalic
		FontName		= .FontName
		FontSize		= .FontSize
		FontStrikeThru	= .FontStrikeThru
		FontUnderLine	= .FontUnderLine
		
		
		.FontBold		= m.obj.FontBold
		.FontItalic		= m.obj.FontItalic
		.FontName		= m.obj.FontName
		.FontSize		= m.obj.FontSize
		.FontStrikeThru	= m.obj.FontStrikeThru
		.FontUnderLine	= m.obj.FontUnderLine
		
		Height	= .TextHeight(m.txt)
		Width	= .TextWidth(m.txt)
		
		.FontBold		= FontBold
		.FontItalic		= FontItalic
		.FontName		= FontName
		.FontSize		= FontSize
		.FontStrikeThru	= FontStrikeThru
		.FontUnderLine	= FontUnderLine
		
	endwith

	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform