Search code examples
inno-setuppascalscript

Display custom page and save inputs to a file only when specific component is selected in Inno setup


I'm trying to build a setup for my application , which contains two parts: server and client. The client part needs to have an IP address entered by the user. I'm using a custom page to prompt for the IP address. But I need to display the custom page, only if user selects "Client" component.

[Components]
Name: "Serveur"; Description: "Server installation"; Types: Serveur; Flags: exclusive; 
Name: "Client"; Description: "Client installation"; Types: Client; Flags: exclusive

[Types]
Name: "Serveur"; Description: "Server Installation"
Name: "Client"; Description: "Client Installation"
[Code]                                                                                                                                    
var
  Page: TInputQueryWizardPage;
  ip: String;

procedure InitializeWizard();
begin
  Page := CreateInputQueryPage(wpWelcome,
    'IP Adresse du serveur', 'par exemple : 192.168.1.120',
    'Veuillez introduire l''adresse IP du serveur :');

  Page.Add('IP :', False);

  Page.Values[0] := ExpandConstant('192.168.x.x');
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if (CurPageID = Page.ID) then
  begin
    ip := Page.Values[0];
    SaveStringToFile('C:\Program Files\AppClient\ipAddress.txt', ip, False);
  end;

  Result := True;
end;

Solution

    1. Your custom page must go only after the "Select Components" page, so you need to pass wpSelectComponents to CreateInputQueryPage:

      var
        Page: TInputQueryWizardPage;
      
      procedure InitializeWizard();
      begin
        Page :=
          CreateInputQueryPage(
            wpSelectComponents, 'IP Adresse du serveur', 'par exemple : 192.168.1.120',
            'Veuillez introduire l''adresse IP du serveur :');
        Page.Add('IP :', False);
        Page.Values[0] := '192.168.x.x';
      end;
      

      (Also note that there's no point in calling ExpandConstant on a string literal that does not include any constants).

    2. Skip the custom page, when the "Client" component is not selected:

      function IsClient: Boolean;
      begin
        Result := IsComponentSelected('Client');
      end;
      
      function ShouldSkipPage(PageID: Integer): Boolean;
      begin
        Result := False;
        if PageID = Page.ID then
        begin
          Result := not IsClient;
        end;
      end;
      

      See also Skipping custom pages based on optional components in Inno Setup.

    3. Well behaving installer should not make any modifications to a system, before the user finally confirms the installation. So make any changes only, once installation really starts, not already when user click "Next" on the custom page.

      Also, you cannot hard-code a path to the file, use {app} constant.

      procedure CurStepChanged(CurStep: TSetupStep);
      var
        IP: string;
      begin
        if (CurStep = ssInstall) and IsClient() then
        begin
          IP := Page.Values[0];
          SaveStringToFile(ExpandConstant('{app}\ipAddress.txt'), IP, False);
        end;
      end;