Required a number in text box in Inno Setup

前端 未结 3 1840
走了就别回头了
走了就别回头了 2020-12-10 17:37

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 i

相关标签:
3条回答
  • 2020-12-10 17:58

    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.

    0 讨论(0)
  • 2020-12-10 18:18

    You can setup the next button to be enabled or disabled in the CurPageChanged event when the user reaches the page where resides your edit box. 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;
    
    0 讨论(0)
  • 2020-12-10 18:18

    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+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 }
              (Key = #3) or (Key = #22) or (Key = #24) or { Ctrl+C, Ctrl+V, Ctrl+X }
              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;
    
    0 讨论(0)
提交回复
热议问题