Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Inputbox

bubblybubbly Member Posts: 18
If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?

Comments

  • GaashiusGaashius Member Posts: 349
    : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    :
    You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • bubblybubbly Member Posts: 18
    : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : :
    : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    :
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    Sorry to be such a pain!

    I have...

    repeat Inputbox(...)
    Until UserInput is an integer

    I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?


    Thanks

  • GaashiusGaashius Member Posts: 349
    [b][red]This message was edited by Gaashius at 2005-4-11 7:22:32[/red][/b][hr]
    : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : :
    : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : :
    : : ****************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    : Sorry to be such a pain!
    :
    : I have...
    :
    : repeat Inputbox(...)
    : Until UserInput is an integer
    :
    : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    :
    :
    : Thanks
    :
    :
    It's not pain for me to help people :-)

    So, the solution is to check if it is an integer is to make a boolean function. In code this will look like:
    [code]
    function checkint(s:string):boolean;
    const
    limit:set of char = ['0'..'9'];
    var
    n:byte;
    begin
    result:=true;
    for n:=1 to length(s) do if (s[n] in limit)=false then result:=false;
    end;
    [/code]

    so your loop needs to be:
    [code]
    repeat inputbox(...)
    ...
    until checkint(userinput);
    [/code]

    Hope this will work

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)




  • bubblybubbly Member Posts: 18
    : [b][red]This message was edited by Gaashius at 2005-4-11 7:22:32[/red][/b][hr]
    : : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : : :
    : : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : : :
    : : : ****************
    : : : Any questions? Just ask!
    : : :
    : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : :
    : : :
    : : :
    : : Sorry to be such a pain!
    : :
    : : I have...
    : :
    : : repeat Inputbox(...)
    : : Until UserInput is an integer
    : :
    : : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    : :
    : :
    : : Thanks
    : :
    : :
    : It's not pain for me to help people :-)
    :
    : So, the solution is to check if it is an integer is to make a boolean function. In code this will look like:
    : [code]
    : function checkint(s:string):boolean;
    : const
    : limit:set of char = ['0'..'9'];
    : var
    : n:byte;
    : begin
    : result:=true;
    : for n:=1 to length(s) do if (s[n] in limit)=false then result:=false;
    : end;
    : [/code]
    :
    : so your loop needs to be:
    : [code]
    : repeat inputbox(...)
    : ...
    : until checkint(userinput);
    : [/code]
    :
    : Hope this will work
    :
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    :
    :
    Great!! That works! Thanks a million!

    It's repeating til the user enters an integer, but it's crashing if the user doesn't enter anything or if they click cancel.

    Any ideas?
  • zibadianzibadian Member Posts: 6,349
    : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : :
    : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : :
    : : ****************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    : Sorry to be such a pain!
    :
    : I have...
    :
    : repeat Inputbox(...)
    : Until UserInput is an integer
    :
    : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    :
    :
    : Thanks
    :
    :
    I would use exception handling in this case. A repeat-until loop will lock-out windows and not allow the user to enter anything. Here is a small example:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    var
    UserInput: integer;
    begin
    try
    UserInput := StrToInt(Edit1.Text);
    except
    ShowMessage('You did not enter an integer!');
    Exit;
    end;
    // Some process involving UserInput
    end;
    [/code]
    This way the user can enter anything, and only if he starts to use the value will it be checked.
    If you want to truly check the value entered, you need to check the last letter entered by the user in the OnChange() event of the edit. This is somewhat more complex:
    [code]
    procedure TForm1.Edit1Change(Sender: TObject);
    var
    i: integer;
    begin
    i := Edit1.SelStart; // Remember the position
    if Edit1.SelStart > 0 then // Check if there are letters
    if not (Edit1.Text[Edit1.SelStart] in ['0'..'9']) then
    begin // if the last letter enter is not a number
    Edit1.Text := StringReplace(Edit1.Text, Edit1.Text[edit1.SelStart]
    , '',[]); // Remove it
    Edit1.SelStart := i-1; // Reposition the text-cursor to its original position
    end;
    end;
    [/code]
    This will stop any "illegal" characters from being entered. It has its drawbacks however: the user can still copy/paste anything into the edit box.
    I would suggest both method to be on the safe side. The latter to disallow the entering of any "illegal" characters, the former to trap the remaining possible errors.
  • zibadianzibadian Member Posts: 6,349
    : : [b][red]This message was edited by Gaashius at 2005-4-11 7:22:32[/red][/b][hr]
    : : : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : : : :
    : : : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : : : :
    : : : : ****************
    : : : : Any questions? Just ask!
    : : : :
    : : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : : :
    : : : :
    : : : :
    : : : Sorry to be such a pain!
    : : :
    : : : I have...
    : : :
    : : : repeat Inputbox(...)
    : : : Until UserInput is an integer
    : : :
    : : : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    : : :
    : : :
    : : : Thanks
    : : :
    : : :
    : : It's not pain for me to help people :-)
    : :
    : : So, the solution is to check if it is an integer is to make a boolean function. In code this will look like:
    : : [code]
    : : function checkint(s:string):boolean;
    : : const
    : : limit:set of char = ['0'..'9'];
    : : var
    : : n:byte;
    : : begin
    : : result:=true;
    : : for n:=1 to length(s) do if (s[n] in limit)=false then result:=false;
    : : end;
    : : [/code]
    : :
    : : so your loop needs to be:
    : : [code]
    : : repeat inputbox(...)
    : : ...
    : : until checkint(userinput);
    : : [/code]
    : :
    : : Hope this will work
    : :
    : : ****************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    : :
    : :
    : Great!! That works! Thanks a million!
    :
    : It's repeating til the user enters an integer, but it's crashing if the user doesn't enter anything or if they click cancel.
    :
    : Any ideas?
    :
    Here is a better function, which can also be adopted to enter a float:
    [code]
    function SafeConvert(s: string; var i: integer): boolean;
    begin
    try
    i := StrToInt(s);
    Result := true;
    except
    Result := false;
    end;
    end;
    [/code]
    This function can be used in the same way as CheckInt(), but it also returns the converted value.
  • bubblybubbly Member Posts: 18
    : : : [b][red]This message was edited by Gaashius at 2005-4-11 7:22:32[/red][/b][hr]
    : : : : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : : : : :
    : : : : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : : : : :
    : : : : : ****************
    : : : : : Any questions? Just ask!
    : : : : :
    : : : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : : : :
    : : : : :
    : : : : :
    : : : : Sorry to be such a pain!
    : : : :
    : : : : I have...
    : : : :
    : : : : repeat Inputbox(...)
    : : : : Until UserInput is an integer
    : : : :
    : : : : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    : : : :
    : : : :
    : : : : Thanks
    : : : :
    : : : :
    : : : It's not pain for me to help people :-)
    : : :
    : : : So, the solution is to check if it is an integer is to make a boolean function. In code this will look like:
    : : : [code]
    : : : function checkint(s:string):boolean;
    : : : const
    : : : limit:set of char = ['0'..'9'];
    : : : var
    : : : n:byte;
    : : : begin
    : : : result:=true;
    : : : for n:=1 to length(s) do if (s[n] in limit)=false then result:=false;
    : : : end;
    : : : [/code]
    : : :
    : : : so your loop needs to be:
    : : : [code]
    : : : repeat inputbox(...)
    : : : ...
    : : : until checkint(userinput);
    : : : [/code]
    : : :
    : : : Hope this will work
    : : :
    : : : ****************
    : : : Any questions? Just ask!
    : : :
    : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : :
    : : :
    : : :
    : : :
    : : :
    : : Great!! That works! Thanks a million!
    : :
    : : It's repeating til the user enters an integer, but it's crashing if the user doesn't enter anything or if they click cancel.
    : :
    : : Any ideas?
    : :
    : Here is a better function, which can also be adopted to enter a float:
    : [code]
    : function SafeConvert(s: string; var i: integer): boolean;
    : begin
    : try
    : i := StrToInt(s);
    : Result := true;
    : except
    : Result := false;
    : end;
    : end;
    : [/code]
    : This function can be used in the same way as CheckInt(), but it also returns the converted value.
    :


    I've got the function in the program, and I'm calling it by

    Repeat...
    until SafeConvert(UserInput) = true

    It says not enough parameters. What am I missing?


  • zibadianzibadian Member Posts: 6,349
    [b][red]This message was edited by zibadian at 2005-4-11 23:57:47[/red][/b][hr]
    : : : : [b][red]This message was edited by Gaashius at 2005-4-11 7:22:32[/red][/b][hr]
    : : : : : : : If I want to get text from the user I can use an inputbox. But, I want to ask how manu lines they want. What do I use that will take in an integer?
    : : : : : : :
    : : : : : : You need the inputted string to be formatted to an integer, for eg. maybe the StrToInt function.
    : : : : : :
    : : : : : : ****************
    : : : : : : Any questions? Just ask!
    : : : : : :
    : : : : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : : : : :
    : : : : : :
    : : : : : :
    : : : : : Sorry to be such a pain!
    : : : : :
    : : : : : I have...
    : : : : :
    : : : : : repeat Inputbox(...)
    : : : : : Until UserInput is an integer
    : : : : :
    : : : : : I know how to do "until UserInput = 'abcd'", but how do I get it to repeat until it is an integer?
    : : : : :
    : : : : :
    : : : : : Thanks
    : : : : :
    : : : : :
    : : : : It's not pain for me to help people :-)
    : : : :
    : : : : So, the solution is to check if it is an integer is to make a boolean function. In code this will look like:
    : : : : [code]
    : : : : function checkint(s:string):boolean;
    : : : : const
    : : : : limit:set of char = ['0'..'9'];
    : : : : var
    : : : : n:byte;
    : : : : begin
    : : : : result:=true;
    : : : : for n:=1 to length(s) do if (s[n] in limit)=false then result:=false;
    : : : : end;
    : : : : [/code]
    : : : :
    : : : : so your loop needs to be:
    : : : : [code]
    : : : : repeat inputbox(...)
    : : : : ...
    : : : : until checkint(userinput);
    : : : : [/code]
    : : : :
    : : : : Hope this will work
    : : : :
    : : : : ****************
    : : : : Any questions? Just ask!
    : : : :
    : : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : : Great!! That works! Thanks a million!
    : : :
    : : : It's repeating til the user enters an integer, but it's crashing if the user doesn't enter anything or if they click cancel.
    : : :
    : : : Any ideas?
    : : :
    : : Here is a better function, which can also be adopted to enter a float:
    : : [code]
    : : function SafeConvert(s: string; var i: integer): boolean;
    : : begin
    : : try
    : : i := StrToInt(s);
    : : Result := true;
    : : except
    : : Result := false;
    : : end;
    : : end;
    : : [/code]
    : : This function can be used in the same way as CheckInt(), but it also returns the converted value.
    : :
    :
    :
    : I've got the function in the program, and I'm calling it by
    :
    : Repeat...
    : until SafeConvert(UserInput) = true
    :
    : It says not enough parameters. What am I missing?
    :
    :
    :
    As you can see in the function definition:
    [code]
    function SafeConvert(s: string; [red]var i: integer[/red]): boolean;
    [/code]
    it expects an integer. This parameter will hold the integer value if the convertion was successful.
    Side note: You can also use this in the repeat-until loop:
    [code]
    repeat
    until SafeConvert(UserInput, SomeInteger);
    [/code]
    because Delphi expects a boolean after until, which is exactly what the function returns.

    Side note II: Delphi's debugger will ALWAYS show exceptions. For this reason the code appears not to work properly when run from inside Delphi, but if you run it as a normal program, you will not see an error message.
Sign In or Register to comment.