Home > Net >  Is there a way I can validate multiple inputs through InputBox for integers?
Is there a way I can validate multiple inputs through InputBox for integers?

Time:11-02

I am basically new to Delphi and I have been given an assignment where I have to sort an integer array, and also validate every input.

I have taken inputs of the array through the InputBox() function. The code for which goes like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  for i := 0 to Length(arr)-1 do
  begin
    arr[i] := StrToInt(InputBox('Program to sort integers', 'Enter 5 integers', ''));
  end;

I have also tried putting a try..except block to catch errors, but it is not working as expected. The try..except block goes something like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  for i := 0 to Length(arr)-1 do
  begin
    try
      arr[i] := StrToInt(InputBox('Program to sort integers', 'Enter 5 integers', ''));
    except
      ShowMessage('Invalid input');
    end;

The problem is that on clicking the button, the above OnClick event handler gets executed, but the except block doesn't.

What should I now do so that input gets validated?

If you want to know more things about my form components, then I’ve added one label, one ListBox to display a sorted array and one button (which you know by name as Button1.)

Thanks in advance.

CodePudding user response:

Using the OnKeyPress() event as you show in your comment! But, if you want that the user can simply hit Enter to mark the end of a value, you also need to accept #13 as a valid key.

Thus, the first test in the OnKeyPress event becomes

if not (Key in [#8, #13, '0'..'9']) then
begin
  Key := #0;
  Exit;
end;

Then you need to handle the #13 key: (pseudocode)

if key = #13 then
begin
  // get the value of entered digits using `StrToInt()` function
  // to generalize, use `TEdit(Sender)` to refer to the current edit control
  // verify value against min and max values
  // if previous test succeeds, 
    // add to array
    // clear the edit control text
  // else show error msg
end

All other accepted keys are handled by the control.

CodePudding user response:

You can't restrict InputBox but you can make your own InputBox with a simple form with Edit box and label and buttons.
To restrict edit box to accept only integers of certain range you have to options :

  1. Make a new Edit class inherited from TEdit


      TIntegerEdit = class(TEdit)
      protected
        procedure KeyDown(var Key: Word; Shift: TShiftState); override;
        procedure KeyUp(var Key: Word; Shift: TShiftState); override;
        procedure KeyPress(var Key: Char); override;
        procedure DoEnter; override;
        procedure DoExit; override;
      end;

2)Use TEdit events to reach your goal. Look at the following sample.

Our form contains an edit control and two buttons. The Edit OnKeyUp & OnKeyDown use the same event.

  TForm1 = class(TForm)
    Edt1: TEdit;
    BtnOk: TButton;
    BtnCancel: TButton;
    procedure Edt1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edt1KeyPress(Sender: TObject; var Key: Char);
    procedure Edt1Enter(Sender: TObject);
    procedure Edt1Exit(Sender: TObject);
  private
    { Private declarations }
    FOldText : string;
  public
    { Public declarations }
  end;

Here is the code used to restrict the Edit to range 1000 to 5000.



    procedure TForm1.Edt1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin
      // if not in range '0' .. '9' , VK_BACK, VK_TAB, VK_RETURN
      if not ((Key in [48..57]) or (Key in [VK_BACK, VK_TAB, VK_RETURN])) then
        Key := 0;
    end;
    
    procedure TForm1.Edt1KeyPress(Sender: TObject; var Key: Char);
    var
      i : Integer;
    begin
      if not ((Key in ['0'..'9']) or (Ord(Key) in [VK_BACK, VK_TAB, VK_RETURN])) then
        Key := #0;
    end;
    
    procedure TForm1.Edt1Enter(Sender: TObject);
    begin
      FOldText := '';
      Edt1Exit(Self);
      FOldText := Edt1.Text;
    end;
    
    procedure TForm1.Edt1Exit(Sender: TObject);
    const
      MinVal = 1000;
      MaxVal = 5000;
    var
      IntVal, E: Integer;
    begin
      Val(Edt1.Text, IntVal, E);
      if E  0 then //Do we have an error?
        Edt1.Text := FOldText;
    
      if (IntVal  MaxVal) then
        Edt1.Text := FOldText;
    end;

  • Related