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!

Entering/Formatting Currency TEdit/TMaskEdit

Status
Not open for further replies.

sijgs

Programmer
Dec 29, 2001
100
CA
Oh, help...

I have a TEdit box in which I want to enter currency. I want the thing to start out looking like:

$0.00

and enter the digit '1' and get

$0.01

then enter the digit '2' and get

$0.12

I'm not really concerned about getting the ',' like:

$1,234.56 (although that would be nice...)

I've tried every combination of an editmask in a TMaskEdit but can't get it to enter Right-To-Left...

I've read the thread that suggests Woll2Wall, but not for $xxx.00

I've read the thread that suggests seltext, sellength.. but that doesn't make any sense that it has to be that hard.

I've tried everything I can with KeyUp/Down/Press but always end up with the character showing like:

2$0.01

Before Keyup gets control which is "aesthetically" displeasing...

IS IT REALLY THIS HARD OR HAS MY MIND GONE TO MARS?

Thanks for any suggestions!

JGS
 
Are you sure you really want to do this?

It could be most confusing for the user because TEdit will show a cursor and that indicates the position where the user will expect the character to appear. For example, if the operator positions the cursor immediately to the right of the dollar sign and enters a '9' then the user would expect to see either '$9.00' or '$90.00' (depending on Insert status).

You don't specify if the user should enter the decimal point or not.

Suppose the user makes a mistake. Are you allowing the user to correct mistakes with backspace and/or Delete and precisely how do you want those keys actioned?

Anyway, it is not very difficult to implement what you've asked for. You should really define a new component derived from TEdit. Assuming that backspace and delete keys have no effect then what you need do is something like:

Define a private integer variable called 'edvalue' which will hold the money as cents.

In your FormCreate procedure code the following:
Code:
 edvalue := 0;
 edit1.text := '$0.00';
 edit1.ReadOnly := true;
 edit1.SelStart := Length(edit1.text);

Define a KeyPress event handler as follows:
Code:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
 digit: integer;
 code: integer;
begin
 Val ( key, digit, code );
 if code = 0 then begin
  edvalue := edvalue * 10 + digit;
  edit1.text := Format ( '$%d.%.2d', [ edvalue div 100, edvalue mod 100 ] );
 end;
 edit1.SelStart := Length(edit1.text);
 key := #0;
end;

You will need to do some error checking to ensure that edvalue does not exceed the maximum for an integer. You will need to decide how the field should be cleared back to $0.00. You may want to disable the left and right cursor control keys.

Hope that helps.


Andrew

 
Andrew:

Thanks for the tips, I'm working on the "routines" that I can implement for all of the currency fields. I'm afraid I'm an old style programmer, never learned enough about component design or classes to do it that way, so it's "old style subroutines" that everyone calls.

You also mentioned "how it works".. it is emulating a cash register which has no backspace key (but I am letting them use on on this keyboard) but does have a "clear" key which a PC KB doesn't so I implemented "delete" to mean "clear".

Only problem I can't seem to solve now is:

If the text in the editbox isn't already set to $0.00 (I will find all the culprits eventually....) "on enter" sets it to $0.00 and then sets selstart but the cursor always ends up just past the $ sign...

Thanks for the pointers...

Regards,
JGS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top