user11955
user11955

Reputation: 63

Required a number in text box in Inno Setup

I found a code here that I needed. To only allow write numbers in a text box. But I still wanted more, which does not offer up the "Next" button without write the number in this text box.

Can help me?

procedure NumbersOnly(Sender: TObject; var Key: Char);
var
  S: string;
begin
  S := ('1234567890'#8);
  if Pos(Key, S) = 0 then 
    Key := #0;
end;

Upvotes: 6

Views: 4936

Answers (3)

Angel Montes de Oca
Angel Montes de Oca

Reputation: 1130

This is the way I use based on keys from ASCII code:

procedure justNumbers(Sender: TObject; var Key: Char);
begin
  if not ((Key = #8) or (Key = #43) or ((Key >= #48) and (Key <= #57)))
          then
  begin
    Key := #0;
  end;
end; 

Could work for validate others characters too.

Upvotes: 0

TLama
TLama

Reputation: 76713

You can setup the next button to be enabled or disabled in the CurPageChanged event when the user reaches the page where your edit box resides. Except that you need to monitor changes of that edit box to enable or disable the next button according to whether there's something entered in that edit box. For this you need to write a handler for the OnChange event. Here is an example:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
var
  MyEdit: TNewEdit;
  MyPage: TWizardPage;

procedure MyEditChange(Sender: TObject);
begin
  // enable the next button if the edit box is not empty; disable otherwise
  WizardForm.NextButton.Enabled := MyEdit.Text <> '';
end;

procedure MyEditKeyPress(Sender: TObject; var Key: Char);
var
  KeyCode: Integer;
begin
  // allow only numbers
  KeyCode := Ord(Key);
  if not ((KeyCode = 8) or ((KeyCode >= 48) and (KeyCode <= 57))) then
    Key := #0;
end;

procedure InitializeWizard;
begin
  MyPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');

  MyEdit := TNewEdit.Create(WizardForm);
  MyEdit.Parent := MyPage.Surface;
  MyEdit.Left := 0;
  MyEdit.Top := 0;
  MyEdit.Width := 150;
  MyEdit.OnChange := @MyEditChange;
  MyEdit.OnKeyPress := @MyEditKeyPress;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // if the currently turned wizard page is the one with the edit box, enable
  // the next button if the edit box is not empty; disable otherwise
  if CurPageID = MyPage.ID then
    WizardForm.NextButton.Enabled := MyEdit.Text <> '';
end;

Upvotes: 9

Martin Prikryl
Martin Prikryl

Reputation: 202534

There are two flaws in the @TLama's answer:

  • A user can bypass the check by using Paste command from edit box context menu (and possibly by other kinds of input methods).

    To fix this, you can introduce a last resort check in NextButtonClick.

    I would also suggest adding the check for an empty input there, instead of MyEditChange, as it allows providing feedback to the user, explaining what is wrong.

  • On the other hand, Ctrl+A, Ctrl+C, Ctrl+V and Ctrl+X keys are not working. Particularly a lack of Ctrl+V is not comfortable.

    To fix this, allow these control characters in the MyEditKeyPress.

[Code]

var
  MyEdit: TNewEdit;
  MyPage: TWizardPage;

procedure ValidateMyEdit;
begin
  // enable the next button if the edit box is not empty; disable otherwise
  WizardForm.NextButton.Enabled := (MyEdit.Text <> '');
end;  

procedure MyEditChange(Sender: TObject);
begin
  ValidateMyEdit;
end;

function IsDigit(C: Char): Boolean;
begin
  Result := (C >= '0') and (C <= '9')
end;

procedure MyEditKeyPress(Sender: TObject; var Key: Char);
begin
  if not ((Key = #8) or // Tab key
          // Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
          (Key = #1) or (Key = #3) or (Key = #22) or (Key = #24) or
          IsDigit(Key)) then
  begin
    Key := #0;
  end;
end;

procedure InitializeWizard;
begin
  MyPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');

  MyEdit := TNewEdit.Create(WizardForm);
  MyEdit.Parent := MyPage.Surface;
  MyEdit.Left := 0;
  MyEdit.Top := 0;
  MyEdit.Width := 150;
  MyEdit.OnChange := @MyEditChange;
  MyEdit.OnKeyPress := @MyEditKeyPress;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = MyPage.ID then
  begin
    ValidateMyEdit;
  end;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  I: Integer;
begin
  Result := True;

  if CurPageID = MyPage.ID then
  begin
    for I := 1 to Length(MyEdit.Text) do
    begin
      if not IsDigit(MyEdit.Text[I]) then
      begin
        MsgBox('Only numbers are allowed', mbError, MB_OK);
        Result := False;
        Break;
      end;
    end;
  end;
end;

Upvotes: 5

Related Questions