SystemVal (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses the System Val function to convert a string to an integer. Enter an integer in the text edit and then click the button. An error occurs if the value entered is not an integer.

Code

procedure TForm1.Button1Click(Sender: TObject);
var
  I, Code: Integer;
  mystr: string;
begin
  { Get text from TEdit control. }
  mystr := Edit1.Text;
  Val(mystr, I, Code);
  { Possible error during conversion to integer }
  if Code <> 0 then
    MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0, mbOk)
  else
    Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;

Uses