?
Solved

number validations OnExit edit1.text

Posted on 2003-03-23
14
Medium Priority
?
628 Views
Last Modified: 2010-04-16
Hi guys, this is my 1st post!:)

I just finished my 1st calculator on delphi 6 (im new on delphi!) and now i dont want to have letters or non-numbers caracters when the user OnExit'ing the edit1.text .

i have tryed :

if (edit1.text <> '1..999999')
 then
  begin
   showmessage('The caracter isnt a number, plz retry!');
   edit1.Text:='';
  end;

but it seems to not work cause it shows me the "message" everytime i enter a number!  can anyone help me plz!

another question: if im using a messageDlg, is there a way to only reading numbers?

thx a lot!
0
Comment
Question by:mediteranio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 3

Expert Comment

by:ILE
ID: 8191641
a plently of combination u mey use a maskedit

instead a textedit

end put in editmask something like 99999999999999

now the edit box will only accept numbers


another solution

work just when u not in id (when you run just program }

do what is writen;



   s:string;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
s:='';
end;


procedure TForm1.Edit1Change(Sender: TObject);
var r :real;
begin
try
r:=strtofloat(edit1.text);
except
edit1.text:=s;
end;
s:=edit1.text;
end;

end.




0
 
LVL 34

Expert Comment

by:Slick812
ID: 8192800
hello mediteranio, a good way to have control of what goes into an Edit (or memo) is the OnKeyPress Event, you get waht key has been pressed in thr Key parameter, , and then you can allow that key, change that key (to just capitals) or block that key, if you change the key to #0, it blocks that key


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Ord(Key) = 8 then Exit;
{#8 is the backspace key}
if (Ord(Key) < 48) or (Ord(Key) > 57) then
Key := #0;
end;
0
 
LVL 2

Accepted Solution

by:
Ratje earned 200 total points
ID: 8193460
u can use something like this :

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9','-',#8]) then
    if (Key in [',', '.']) then
     Key := DecimalSeparator
    else
     Key := #0 ;

end;

This way u can use either , or . for decimalseparation .



Rat
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:BLayman
ID: 8193484
I agree with Ratje.

****Do not rely on exception checking when you can do a simple if statement.****  

Exception checking (Try Finally/Except End) is INCREDIBLY SLOW compared to one if statement.  If you get in the habit now, you'll never stop and sometime down the road you'll find some repeated routine is taking minutes when it should have been done in seconds.

Also watch for calls like StrToIntDef that do exactly the same thing as the try block just in delphi's code instead of yours.
0
 
LVL 3

Expert Comment

by:D-Master
ID: 8193553
onExit

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
 begin
  showmessage('The caracter isnt a number, plz retry!');
  edit1.Text:='';
  activecontrol := edit1;
 end;
0
 
LVL 3

Expert Comment

by:D-Master
ID: 8193634
onExit

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
 begin
  showmessage('The caracter isnt a number, plz retry!');
  edit1.Text:='';
  activecontrol := edit1;
 end;
0
 

Expert Comment

by:BLayman
ID: 8193763
ummm...
strtoint('a') = baboom
0
 
LVL 3

Expert Comment

by:D-Master
ID: 8194056
first use what Ratje said to make sure that the text in edit is a number
-------------------------------------------
then to make sure that the number he entered is in the range you want it to be in, use this

if (strtoint(edit1.text) < 1) or (strtoint(edit1.text) > 999999)
then
begin
 showmessage('The caracter isnt a number, plz retry!');
 edit1.Text:='';
 activecontrol := edit1;
end;
0
 

Expert Comment

by:BLayman
ID: 8194093
Ah...
0
 

Author Comment

by:mediteranio
ID: 8208042
thx a lot guys!! im sorry i had some problems with my email so i wasnt able to go check my current password lol
0
 

Expert Comment

by:Churo
ID: 8240519
try


procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if not ( Key in ['+', '-', '.', '0'..'9'] ) then
  begin
    Key := #0;
    MessageBeep(MB_ICONEXCLAMATION);
  end
  else inherited KeyPress(Key);
end;


with this code only will accept + , - , . and numbers, when try to press onother key ... BEEP !
0
 

Expert Comment

by:CleanupPing
ID: 9316634
mediteranio:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question