Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need Some Help About Numeric TextBox Class
Message
De
18/04/2006 20:31:36
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01114408
Message ID:
01114454
Vues:
13
Below is a class that I wrote to handle numeric input. Allows definition for amount decimal and separator:
**************************************************
*-- Class:        moneyinput
*-- ParentClass:  textbox
*-- BaseClass:    textbox
*-- Time Stamp:   04/18/06 08:24:12 PM
*
DEFINE CLASS moneyinput AS textbox
	FontSize = 11
	Alignment = 1
	Value = 0.00
	Height = 23
	InputMask = ""
	SelectOnEntry = .T.
	Width = 100
	*-- XML Metadata for customizable properties
	_memberdata = [<VFPData><memberdata name="amount" type="property" display="Amount"/><memberdata name="decptsym" type="property" display="DecPtSym"/><memberdata name="separatorsym" type="property" display="SeparatorSym"/><memberdata name="addseparator" type="method" display="AddSeparator"/><memberdata name="numdecimals" type="property" display="NumDecimals"/></VFPData>]
	*-- Numeric amount
	amount = 0.00
	*-- Decimal point symbol
	decptsym = .
	*-- Symbol for separating thousands
	separatorsym = ","
	*-- Number of decimals for currency value
	numdecimals = 2
	Name = "moneyinput"


	*-- Adds the separator to the thousands
	PROCEDURE addseparator
		LPARAMETERS pcNumber,pnNumDecimals
		LOCAL llNegative, lnNumber, lnPower, lnLen
			IF VARTYPE(pcNumber) = 'C'
				pcNumber = ALLTRIM(pcNumber)
				IF LEFT(pcNumber,1) = '-'
					llNegative = .T.
					pcNumber   = SUBSTR(pcNumber,2)
				ELSE
					llNegative = .F.
				ENDIF
				lnNumber = VAL(pcNumber)
			ELSE
				IF VARTYPE(pnNumDecimals) = 'L'
					pnNumDecimals = this.NumDecimals
				ENDIF
				IF pcNumber < 0
					llNegative = .T.
					pcNumber   = ABS(pcNumber)
				ELSE
					llNegative = .F.
				ENDIF
				lnNumber = pcNumber
				lnPower  = -1
				DO WHILE lnNumber > 1
					lnNumber = lnNumber / 10
					lnPower  = lnPower + 1
				ENDDO
				lnNumber = pcNumber
				pcNumber = ALLTRIM(STR(pcNumber,lnPower+pnNumDecimals+2,pnNumDecimals))
			ENDIF
			IF lnNumber = 0.00
				lcNumber = PADR("0"+this.DecPtSym,pnNumDecimals+2,"0")
			ELSE
				lnDecimal = ATC(this.DecPtSym,pcNumber)
				IF lnDecimal > 0
					lcNumber = SUBSTR(pcNumber,lnDecimal)
					pcNumber = LEFT(pcNumber,lnDecimal-1)
				ELSE
					lcNumber = ""
				ENDIF
				lnLen = LEN(pcNumber)
				DO WHILE lnLen >= 4
					lcNumber = this.SeparatorSym + RIGHT(pcNumber,3) + lcNumber
					pcNumber = LEFT(pcNumber,LEN(pcNumber)-3)
					lnLen = LEN(pcNumber)
				ENDDO
				lcNumber = pcNumber + lcNumber
			ENDIF
			IF SUBSTR(lcNumber,LEN(lcNumber)-1,1) = this.DecPtSym
				lcNumber = lcNumber + "0"
			ENDIF
			IF llNegative
				lcNumber = "- " + lcNumber
			ENDIF
			IF LEFT(lcNumber,1) = this.DecPtSym
				lcNumber = "0" + lcNumber
			ENDIF
			IF ATC(this.DecPtSym,lcNumber) = 0
				lcNumber = lcNumber + this.DecPtSym + REPLICATE("0",pnNumDecimals)
			ENDIF
		RETURN lcNumber
	ENDPROC


	PROCEDURE Valid
		IF VARTYPE(this.value) = 'C'
			this.Amount = VAL(STRTRAN(this.value,this.SeparatorSym,""))
		ELSE
			this.Amount = this.Value
		ENDIF
		this.Value = this.AddSeparator(this.Amount,this.NumDecimals)
	ENDPROC


	PROCEDURE Init
		this.Value = "0" + this.DecPtSym + "00"
	ENDPROC


	PROCEDURE KeyPress
		LPARAMETERS nKeyCode, nShiftAltCtrl
		IF !INLIST(nKeyCode,4,9,13,19,27,127)     && Allow ENTER, TAB, CURSOR LEFT/RIGHT, ESC, & BKSP KEYS
			IF !(CHR(nKeyCode) $ "0123456789.,-")
				NODEFAULT
			ENDIF
		ENDIF
		IF nKeyCode = 7                           && DEL key
			this.Value = "0" + this.DecPtSym + "00"
			this.SelStart = 0
			this.SelLength = 4
		ENDIF
	ENDPROC


ENDDEFINE
*
*-- EndDefine: moneyinput
**************************************************
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform