Validate data on custom page when Next button is clicked in Inno Setup

橙三吉。 提交于 2019-12-12 09:57:38

问题


I have managed to get a basic script working to display a wizard (using CreateInputFilePage) for a user to identify a file location that I use to update some settings in an XML file. However, I would like to perform some basic checking of the input to the file selected rather than simply accepting whatever the user provides. For example displaying a message box if the user attempts to press **Next"* when the content is invalid. I am not entirely sure how to handle events arising from the wizard and how to apply any kind of validation rules to the data before proceeding to the next task. Currently, I have defined a simple InitializeWizard procedure.

[Code]
var
  Page: TInputFileWizardPage;

procedure InitializeWizard;
begin
  { wizard }
  Page := CreateInputFilePage(
    wpWelcome, 'Select dFile Location', 'Where is dFile located?',
    'Select where dFile.dba file is located, then click Next.' );

  { Add item (with an empty caption) }
  Page.Add('location of dFile.dba', '*.dba|*.*', '.dba' );
end;

I then recover the file name and location when the CurStepChanged event is triggered and use this to update the some settings in an XML file

procedure CurStepChanged(CurStep: TSetupStep);
var
  dFull: String;
  dPath: String;
  dName: String;
begin
  if (CurStep = ssPostInstall) then
  begin
    { recover dFile location }
    dFull:= Page.Values[0];

    dPath := ExtractFilePath( dFull );
    dName := ExtractFileName( dFull );

    { write dFile location and name to settings.xml }
    UpdateSettingsXML( dPath, 'dFileDirectory' );
    UpdateSettingsXML( dName, 'dFileName' );
  end;
end;

回答1:


You can use OnNextButtonClick event of your custom TWizardPage to do your validation:

function FileIsValid(Path: string): Boolean;
begin
  Result := { Your validation };
end;

var
  Page: TInputFileWizardPage;

function FilePageNextButtonClick(Sender: TWizardPage): Boolean;
begin
  Result := True;
  if not FileIsValid(Page.Values[0]) then
  begin
    MsgBox('File is not valid', mbError, MB_OK);
    Result := False;
  end;
end;

procedure InitializeWizard;
begin
  Page := CreateInputFilePage(...);

  Page.Add(...);

  Page.OnNextButtonClick := @FilePageNextButtonClick;
end;

For an alternative approach, see Inno Setup Disable Next button when input is not valid.



来源:https://stackoverflow.com/questions/56263983/validate-data-on-custom-page-when-next-button-is-clicked-in-inno-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!