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

entering integers at runtime

Status
Not open for further replies.

mjt39

Technical User
Aug 14, 2000
25
0
0
DE
I use Delphi5 prof.
I want to enter a value to an integer variable via a form at runtime.
(Like in grandpa's Turbo Pascal: var i: integer; begin readln(i); ... )

Question 1: Can I do this directly or do I have to use the StrToInt function?

For the latter, Delphi Help provides an example for IntToStr. (which uses both functions):

Code:
procedure TForm1.Button1Click(Sender: TObject);
  begin
    try
      Label1.Caption := IntToStr(StrToInt(Edit1.Text) * StrToInt(Edit2.Text));
    except
//    on (xxx) do (yyy);
      ShowMessage('You must specify integer values. Please try again.');
  end;
end;
(The // line was supplemented by me)

This code doesn’t work, entering an(alpha-)string instead of an integers into an edit box causes an error break, returns to the project editor and shows the error message
“Project p.exe raised exception class EConvertError with message ‘’[string]’ is not a valid integer value’. Process stopped. Use Step or Run to continue.”
The program does set forth after the breakpoint (ShowMessage...).
Setting (xxx) to EConverError and (yyy) to [blank] was no remedy, nor were other experiments.

Question 2. Can I avoid this errorbreak?
 
Try ... except ... " should work OK. However if you are running it from within the Delphi IDE, then I think it depends on how your debugger options (under Tools) are set.

Try running the same piece of code as an application from the desktop, not from the IDE, and see what happens then.

Peter
 
You might find the StrToIntDef function useful. It doesn't cause an exception if it is passed an invalid value.
E.g.
Code:
const
  BadValue = High(Integer);
var
  n: integer;
begin
  ...
  Edit1.Text := 'Rubbish';
  n := StrToIntDef( Edit1.Text, BadValue );
  if n = BadValue then
    ...
would set n to BadValue

Andrew
Hampshire, UK
 
a text box text property is a string so you need
inttostr(edit1.text) to turn the string "10" into integer 10.

i dont think theres a function to turn the string "ten" into integer 10.




Aaron Taylor
John Mutch Electronics
 
There are other ways to ensure that entry is as you require:
[ul]
[li]Use a TMaskEdit component - this allows you create an edit mask, which can then be handled without any other code - the component handles the checking. Just convert the resulting MaskEdit.Text to your integer[/li]
[li]In D6 & D7 (not sure about D5) there is a TSpinEdit component (in the Samples tab) - this allows you to either type in a number or scroll up/down in integer increments, and you can select the upper/lower limits (MaxValue, MinValue) and the length. Results are returned in TSpinEdit.Value. This will give you direct access to your integer, with all the checking done by the component[/li]
[/ul]

Hope that helps

Cheers,
Chris [pc2]
 
Thanks to you all for your help. Peter, you straightened the nail and hit it home in one stroke. An executable file does compile and run on the win desktop. Adjustments in the debugger let my project run in the IDE. It was a warning I got, not an error break.

Andrew, your suggestion is fine if there is a default value to work with or to be caught.

Michael
(1947 - 1951: Alverstoke, Hampshire, UK. 1951 - present: Munich, Berchtesgaden, Goettingen etc., Germany.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top