How to disable NextButton when file is not selecte

2019-02-11 07:17发布

问题:

My Inno Setup program have a custom "input file page" that was created using CreateInputFilePage.

How can I disable the NextButton in this page until the file path is properly picked by the user?

In other words, I need to make the NextButton unclickable while the file selection form is empty, and clickable when the file selection form is filled.

Thank you.

回答1:

The easiest way is to use NextButtonClick to validate the inputs and display an error message when the validation fails.

var
  FilePage: TInputFileWizardPage;

procedure InitializeWizard();
begin
  FilePage := CreateInputFilePage(wpSelectDir, 'caption', 'description', 'sub caption');
  FilePage.Add('prompt', '*.*', '.dat');
end;

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

  if (CurPageID = FilePage.ID) and
     (Length(FilePage.Edits[0].Text) = 0) then
  begin
    MsgBox('Please select a file.', mbError, MB_OK);
    WizardForm.ActiveControl := FilePage.Edits[0];
    Result := False;
  end;
end;

If you really want to update the "Next" button state while the input changes, it is a bit more complicated:

procedure FilePageEditChange(Sender: TObject);
begin
  WizardForm.NextButton.Enabled := (Length(TEdit(Sender).Text) > 0);
end;

procedure FilePageActivate(Sender: TWizardPage);
begin
  FilePageEditChange(TInputFileWizardPage(Sender).Edits[0]);
end;

procedure InitializeWizard();
var
  Page: TInputFileWizardPage;
  Edit: TEdit;
begin
  Page := CreateInputFilePage(wpSelectDir, 'caption', 'description', 'sub caption');
  { To update the Next button state when the page is entered }
  Page.OnActivate := @FilePageActivate;

  Edit := Page.Edits[Page.Add('prompt', '*.*', '.dat')];
  { To update the Next button state when the edit contents changes }
  Edit.OnChange := @FilePageEditChange;
end;