Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Delphi] Input component for numbers (how to?)

148 views
Skip to first unread message

C. Scherrebeck Mżller

unread,
Jul 6, 1995, 3:00:00 AM7/6/95
to
I want an input component for numbers. The component will not be connected to
any database, and this seems to be a problem.

There are two components on Delphi's speedmenu, 'Edit' and 'MaskEdit', which
can be used for text input (names, dates, phone numbers). I can't find any
component for input of numbers. Am I wrong?

The field must (if possible):

- require numbers.
- use right-alignment.

Best regards
Carsten_Scher...@online.pol.dk

PS: If you write directly to me, you must make a NEW letter (the
answer-letter function do not work on the server, I am connected to.

Richard Ranson

unread,
Jul 6, 1995, 3:00:00 AM7/6/95
to
In article <324042748...@online.pol.dk>, C._Scherrebeck_Mżll...@online.pol.dk (C. Scherrebeck Mżller) says:
Please note for the future that Delphi questions should be in
comp.pascal.misc.

>
>There are two components on Delphi's speedmenu, 'Edit' and 'MaskEdit', which
>can be used for text input (names, dates, phone numbers). I can't find any
>component for input of numbers. Am I wrong?

Your right, there is no NumberEdit of equivalent. Now you could scout
about to see if someone has derived a new component from TEdit, I have
seen something called MonyBox which goes part way. You can also do
a great deal with TEdit,the new format conversion routines and the exception
handeling arrangements.

For example I use this function to check real inputs to a TEdit:

Function ValidateReal( Inp : TEdit; S :String; Min, Max : Double) : Boolean;
{ Check whether a TEdit.Text field can be converted to a real }
{ Also check whether the value that results is within a given range }
( The String S is added to the error message if the values are out of range }
var X : Double;
Smin, Smax : String;
begin
Result := True; { Hope for the best }
if max<min then { if the max and min are backwards }
begin { swap them }
x := max;
max := min;
min := x
end;
try
X := StrToFloat(Inp.Text); { this is then input field of TEdit }
if (X<min) or (X>max) then
begin
{These values cannot fail to convert I hope!}
Smin := FloatToStrF(Min, ffGeneral, 4, 2);
Smax := FloatToStrF(Max, ffGeneral, 4 ,2);
{ force an exception due to out of range }
raise EConvertError.Create(S+' must be >'+Smin+' and <'+Smax);
end;
except
on E: EconvertError do { if StrToFloat fails you end up here }
begin
MessageDlg(E.Message, mtWarning, [mbOK], 0);
with Inp do
begin
AutoSelect := True; { this highlights the offending input }
SetFocus; { when you return }
end;
Result := False;
end;
end;
end;

Now my preference is to check the inputs ( i.e. several on a form ) when
the form closes but you can also do this for each TEdit by using its
onExit event. So here a form ( dialog box )with several TEdits has
been defined and the FormClose event occurs when the user presses OK
or Cancel

procedure TPassBandBox.FormClose(Sender: TObject;
var Action: TCloseAction);
Var MinF, MaxF : Double;
begin
{ Dialog is closing, if via OK }
{ button then check for valid data }
if ModalResult=mrOK then
begin
if Not ValidateReal(RippInp,'Ripple',0,3) then
begin { Here the input called Ripple must be real }
Action :=caNone; { and between 0 and 3. If not caNone stops the }
Exit { dialog closing and exit drops out of the proc }
end;
if Not ValidateReal(F1Inp,'Lower Frequency',0,90000) then
begin
Action := caNone;
Exit
end
else
MinF := StrToFloat(F1Inp.Text);
if not ValidateReal(F2Inp,'Upper Frequency',0,90000) then
begin
Action := caNone;
Exit
end
else
MaxF := StrToFloat(F2Inp.Text);
if MaxF<MinF then
begin
MessageDlg('Lower Freq must be < Upper Freq', mtWarning, [mbOK], 0);
F1Inp.AutoSelect := True;
F1Inp.SetFocus;
Action := caNone;
end;
end;
end;

Once you have a valid data input you can manipulate the Text field
of the TEdit Component just like any string.

Hope this helps Richard

0 new messages