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

Handling of numerical data entry 1

Status
Not open for further replies.

pmravdin

Programmer
Jul 10, 2002
9
US
I want to have the user enter into 2 data entry fields numbers (integer or decimal). I would use SpinEdit but of course that will not handle decimals.
I am a little surprised that the Properties would make this trivial. Specifying a number with limits max min limits and an error message or non entry of information for NAN. Still I am learning a little Delphi and I am probably overlooking the obvious. Still looking at the books and help files has not made it obvious.
Could you help me gain a little insite into this?
 
pmravdin,

I'm sorry. I'm having trouble following the problem. Are you getting an error message? If so, what is it? Does this error appear consistently or only when certain values are provided? What are those values?

Thanks in advance....

-- Lance
 
Basically I want to let the user enter 2 decimal numbers and the program to multiply them.
2.1 * 4.3
I want to have error states if someone enters not a number. I realize that this is trivial, but I am a little surprised that that there is a component that makes specifies that it be a real number. Or something in ActiveX to do this.
I don't want to reinvent the wheel. Is there something trivial that will do this??
 
You can do the string to float conversion inside a try except block

function IsNumeric(AString:string):double;
begin
try
result := StrToFloat(AString);
except
showmessage('Number required');
end;

If you want to clear the field if the value is not valid, you can pass in the component like this

function IsNumeric(AEdit:TCustomEdit):boolean;
begin
try
result := false;
StrToFloat(AEdit.Text);
result := true;
except
begin
ShowMessage('This field requires a numeric value.');
AEdit.Clear;
AEdit.SetFocus;
end;
end;

 
You could check the keys entered in the onKeyPress event, if the result isn't valid, ignore the key pressed.

procedure TForm1.edit1KeyPress(Sender: TObject; var Key: Char);
begin
if edit1.Text = '' then exit;
if not (key in [#13,'0'..'9','.',',', #8,'-']) then
key := #0;
if key in ['0'..'9','.',',','-'] then
Try
StrToFloat(edit1.Text+key);
except
key := #0;
end;
end;

lou
 
Alternatively, you could just use the TMaskEdit component and set the editmask. Robertio
Alias: Robbie Calder
Software Developer
urc@walkermartyn.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top