Delphi Tutorial


 

Delphi Tutorial - Miscellaneous - Check validation of input real number in TEdit box and format it into two decimal places.

 

Download this example

            In order to check the validation, its better to write procedure. So you can call this procedure for all the Edit components within your project.

 

procedure CheckReal(Var EdBox : TEdit);
Var
    R : Real;
    Code : Integer;
Begin
    { Get text from TEdit control }
    Val(EdBox.Text, R, Code);
    { Val - Converts a string to a numeric representation. }

    { Error during conversion to Real? }
    If Code<>0 Then
    Begin
        R:=0;
        MessageDlg('Please check your input details ?', mtError, [mbOk], 0);
        EdBox.Text := '0.00';
        { Get focus back to edit box }
        EdBox.SetFocus;
    End Else
        { Pass formatted (with two decimal places) real value to text box }
        EdBox.Text := Format('%f', [R]);
    { Format - Returns a formatted string assembled from a format
    string and an array of arguments. }

End;

 

Now you can call above procedure in Edit component OnExit event.

procedure TForm1.Edit1Exit(Sender: TObject);
begin
    CheckReal(Edit1);
end;

 

DOWNLOAD this example

Click here to download this complete example for Delphi 6. (File size 7KB)

Go top

 


 

Previous Page First Page Next Page

 

 
Copyright © 2003 digitalcoding.com. All rights reserved.