![]() |
|
#1
|
|||
|
|||
|
Hello.
This is my first post here, I am back to Delphi coding after few years, still beginner however and I would need some help. I have read few similar articles here and that was useful, but I need more. Here is what I need. My application should allow user to enter only numbers with 2 decimal places like this 123,45. I put several TMemo objects on form because of their right alignment capability. I used code like this: Quote:
Quote:
I know I could make TMaskEdit to do the job, but then I would have to write new component and I am not familiar with that, never done that before and sure would I be able to do it right. ![]() I hope someone will help me with this, thanks in advance.
Last edited by kdalibor; 06-11-2012 at 06:43 AM. |
|
#2
|
|||
|
|||
|
Don't use the global var Postoji but see if the desired sine (','/'.') is in the text using the Pos function
|
|
#3
|
|||
|
|||
|
If you backspace or delete your decimal separator, you won't be able to reenter it !!!
This some old code - I think it works correctly. Code:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
// Valid data
if key in ['0'..'9'] + ['+'] + ['-'..'.'] +[#8] then begin
// Dec point
if key = #46 then
if Pos('.',edit1.text) <> 0 then begin
key := #0;
windows.Beep(1000,200);
end;
// Backspace
if key = #8 then begin
edit1.text:=Copy(edit1.text,1,length(edit1.text)-1);
edit1.SelStart:=length(edit1.text);
key := #0;
end;
end
else begin
key := #0;
windows.Beep(1000,200);
end;
end;
|
|
#4
|
|||
|
|||
|
Thanks a lot, that's what I was looking for.
![]() Any suggestions how to prevent sign "," to be pressed when memo is empty? I need to avoid this ",12345" situation. Last edited by kdalibor; 06-12-2012 at 09:34 AM. |
|
#5
|
|||
|
|||
|
I assume you disregarded my solution and went for the copy paste of marsheng...
What you should do is try to convert it to integer/float... If that fails keep the old value, otherwise use the new... |
|
#6
|
|||
|
|||
|
You will need to do code around the position of , and the decide what you want to do with it. If you have ,1234 do you want 0,1234 or beep and revert to the previous number, clear the string etc. Same thing on how many decimal places you need.
I think it is a great failing of Delphi not to have a Numeric field input. Mask is a waste of time!!! Cheers Wallace |
|
#7
|
|||
|
|||
|
I thank you both, both solutions are helpfull. Marsheng too uses pos function so I considered them to be alike. I used code Marsheng posted just because I am to lazy and it was easier to modify it then to write new one.
![]() I did minor modification to make code do what I want. Also added Quote:
Last edited by kdalibor; 06-12-2012 at 10:14 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|