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

时间:2014-07-24 17:27:29

标签: 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;

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:4)

@ TLama的回答有两个缺点:

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

    要解决此问题,您可以在NextButtonClick中引入最后的手续费。

    我还建议在那里添加空输入检查,而不是MyEditChange,因为它允许向用户提供反馈,解释错误。

  • 另一方面, Ctrl + C Ctrl + V Ctrl + 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 }
          (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;

答案 2 :(得分:1)

这是我使用基于ASCII码密钥的方式:

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; 

也可以用于验证其他角色。