在Inno Setup的文本框中需要一个数字

use*_*955 6 inno-setup pascalscript

我在这里找到了一个我需要的代码.仅允许在文本框中写入数字.但我仍然想要更多,没有提供"下一步"按钮而没有在此文本框中写入数字.

可以帮我?

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

TLa*_*ama 8

CurPageChanged当用户到达编辑框所在的页面时,您可以设置启用或禁用的下一个按钮.除非您需要监视该编辑框的更改,以根据是否在该编辑框中输入了某些内容来启用或禁用下一个按钮.为此,您需要为OnChange事件编写处理程序.这是一个例子:

[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;
Run Code Online (Sandbox Code Playgroud)


Mar*_*ryl 5

@TLama的回答有两个缺陷:

  • 用户可以通过使用编辑框上下文菜单中的粘贴命令(也可能通过其他类型的输入方法)来绕过检查。

    要解决此问题,您可以引入最后的手段 check in NextButtonClick

    我还建议在那里添加对空输入的检查,而不是MyEditChange,因为它允许向用户提供反馈,解释出了什么问题。

  • 另一方面,Ctrl+ACtrl+CCtrl+VCtrl+X键不起作用。尤其是缺少了Ctrl+V就不舒服。

    要解决此问题,请在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;
Run Code Online (Sandbox Code Playgroud)