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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InputMask. Entering and displaying a numeric value with two decimal places 1

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
After all these years I am trying to get my head round the Inputmask property and possibly the Format property.

What I would like is a text box into which I enter, say, (without quotes) “1234.5”

On leaving the box I would like its value to be 1234.5 and I would like the box to be showing 1,234.50 (in fact a money value, although I do not need the £ sign to be displayed), right-aligned in the text box.

During data entry I would prefer that the text box showed exactly what I was entering; so, if I start entering data at the left of the text box, I would prefer it to look that way, not showing the decimal point until I had entered it. It would also be nice if the text box clears when I start entering data, if that is normal practice

I have tried various options. At present I have Input mask = “999,999.99”, Format “K”. This does indeed select the whole field when the text box gets focus. However when I start entering data, I see that the display shows “1 , .” and this is not what I want. Also, if I start entering “1234.5”, when I key in the decimal point it is ignored . After I have entered the 5 the text box shows “123,45<space>.” And again this is not what I want.

I realise that there are thousands of applications which let users enter numbers quite happily!

Andrew
 
Hi Andrew,
You'll need two inputmasks, one asa the field has the focus and one asa the filed loses focus
In the GotFocus() event of the field you may put
This.InputMask = ""​
etc ...​
and in the LostFocus() event you may
This.InputMask = "999,999.99"​
etc ...​
hth
MK
 
Thanks mjcm

That works up to a point. If I have the input mask as blank ("") in Gotfocus(), that does indeed allow me to enter 1234.5.

But if, (in my Lostfocus event), I set the input mask to "999,999.99" (without quotes), the value is displayed, left justified as
123,.5 .

That is, I end up with two decimal points and the 4 has been converted into a comma. I would like to display it as right-justified
1,234.50

Have tried setting the initial value to 0.00 (previously it was blank). Is that what one needs to do?

Andrew
 
Hi Andrew,
You have to be sure that
- the table field is numeric - if it's a variable the initial value must be 0,
- the InputMask is surrounded by quotes e.g. in the LostFocus event This.InputMask = "999,999.99"

hth
MK
 
Andrew,

The behaviour depends on whether the underlying field is numeric or character. I suspect that in this case it is a character field, because you said that you start entering data from the left of the box (it is not right-justified).

But perhaps you could confirm that before we go any further.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just to follow up my previous answer ...

This is what I do for a numeric field:

1. I have a custom property (of the textbox class) called SavedMask.

2. I set the InputMask to 99,999,999.99

3. In the GotFocus, I have this code:

[tt]this.SavedMask = this.InputMask
this.InputMask = STRTRAN(this.SavedMask, ",", "")[/tt]

Essentially, this strips the commas from the input mask.

4. In the LostFocus, I restore the original mask:

[tt]this.InputMask = this.SavedMask[/tt]

This usually has the desired effect. I suggest you give a try and see it if does what you want.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
The essential thing is your default value needs to be numeric. An unbound textbox will always have a text value, on which you can't really apply inputmasks for numeric formatting.

If controlsource is a numeric variable or field it's much simpler already. If you have an unbound field, set the value 0 in the textbox and VFP will only allow entrance of numeric values. Temporarily setting no inputmask or striping off "," during input is a matter of taste.

To clear the value just set Value =0 in the gotfocus. A Format="K" will select all, so the first digit typed in will delete the previous value. This typically is a nice feature if you tab through controls and they don't lose their values, yet you can fastly overwrite. I'd not clear the value on gotfocus, because of this, you don't want to lose values just by tabbing through some controls, do you?

Bye, Olaf.
 
Thanks Mike

In answer to your first question, my textbox had originally had no value defined, which perhaps was why it defaulted to character. This presumably is why on data entry the characters entered at the left. I imagine this was the original source of my troubles, I naively assumed that my 99.99 picture would in some way constrain the value to be deemed internally to be numeric. I now see this was wrong!

I have pretty much adopted your approach of having a different input mask for input and for display.

It does not really matter about the decimal point being displayed at the time of data entry! That again seems to be determined by the input mask - if you want the user to have an option of entering a decimal, VFP insists on showing it - otherwise it does not let the user enter it himself. I had rather wanted the user to be a free spirit, with a blank canvas to work on, but maybe that is not important.

It is all in the past, but perhaps when VFP 11 comes along the designers may give thought to simplifying and clarifying the matter of numeric input!

Andrew
 
My solution to this type of problem is to create my own class. In the textbox class I defined a new property "Amount" and set it's value to 0.00 with the Assign method. In the Assign method, I have the following:

LPARAMETERS txAmount
IF VARTYPE(txAmount) = "N"
this.Amount = txAmount​
this.Value = TRANSFORM(this.Amount, "999,999.99")​
ELSE
WAIT WINDOW "Invalid value entered" NOWAIT​
this.Value = TRANSFORM(this.Amount, "999,999.99")​
this.SetFocus()​
ENDIF

Then the numeric value entered is accessed via the txtBox.Amount property.
 
Thanks ggreen. I would like to try the approach you are suggesting. I have created a class amtxtnum based on the Textbox control, and have given it a property amount. However I am not familiar with the ASSIGN method.

I imagine that I need to put the code you have supplied into one of the methods of amtxtnum. I cannot see an ASSIGN method, but there are methods like Lostfocus() and InteractiveChange(). Can I put the code into one of these?

Sorry to be a bit slow. Andrew
 
When you create the property "Amount" you can also create two methods "Assign" and "Access". See attachment. I am using an add-in for creating properties/methods but the standard VFP dialog also has this capability. Then you will see the new method with the name "Amount_Assign". Add the code to this method.

 
 http://files.engineering.com/getfile.aspx?folder=6bc1d2ce-4cef-4cf3-a5a1-4cc58dbb04c1&file=9-11-2014_10-04-33_AM.jpg
Andrew,

To create an Assign method, proceed as follows:

1. In the class designer, select Edit Property/Method from the Class menu.

2. In the resulting dialogue, select the property for which you want to create the Assign method - in this case, the amount property.

3. Tick the checkbox (to the right of the dialogue) labelled Assign Method; close the dialogue.

4. You will now have a method named amount_assign. It is in that method where you right the code that Mr Green suggested.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
The article by Tamar is an excellent learning document on the use of Assign and Access methods. These are very powerful methods that are available for use.
 
Mrs. Tamar Granor is an extraordinary teacher.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Thank you Tamar.

Shall do so. Still find your "Microsoft Office Automation with VFP" a very useful document.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top