Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-11-2012, 06:39 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default Enter only numbers

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:
procedure TfrmGlavna.edtUkupnaPovrsinaKeyPress(Sender: TObject;
var Key: Char);
begin
if (Postoji = False) and ((Key = ',') or (Key = '.')) then
begin
Key:= Chr(44);
Postoji:= True;
end
else
if not (Key in ['0'..'9', #8]) then
Key := #0;
end;
Quote:
procedure TfrmGlavna.edtUkupnaPovrsinaExit(Sender: TObject);
begin
Postoji:= False;
end;
To explain. Boolean variable Postoji I used to prevent multiple entering of "," sign. Not a happy solution, I admit, but it do the job. Now, I have a problem I can't figured out how to solve. Everything works fine with this until user use BACKSPACE key and delete "," sign. When is that the case I can't enter another "," sign because Postoji is True now.

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  
Old 06-11-2012, 08:26 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

Don't use the global var Postoji but see if the desired sine (','/'.') is in the text using the Pos function
  #3  
Old 06-12-2012, 12:23 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 205
Default

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  
Old 06-12-2012, 09:20 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default

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  
Old 06-12-2012, 09:50 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

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  
Old 06-12-2012, 10:05 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 205
Default

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  
Old 06-12-2012, 10:11 AM
kdalibor kdalibor is offline
Junior Member
 
Join Date: Jun 2012
Posts: 17
Default

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:
if (aKey = #44) and (aMemo.Lines.Text = '') then
begin
aKey:= #0;
Windows.Beep(1000,200);
end;


if aKey = #44 then
if Pos(',',aMemo.Lines.Text) <> 0 then begin
aKey := #0;
Windows.Beep(1000,200);
end;
in my function so it works perfectly now. Thank you both again.

Last edited by kdalibor; 06-12-2012 at 10:14 AM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:43 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.