Inno Setup: Add a Custom Input Field

Abs picture Abs · Jul 9, 2010 · Viewed 23.7k times · Source

I am making use of Inno Setup (its amazing!). I was hoping to customise the installer so that I can accept a string from the user in the form of an input field and maybe add a message to it.

How can I do this? I had a look through the docs, google search and not much came up!

Thanks all for any help

Answer

Thorsten Dittmar picture Thorsten Dittmar · Jul 9, 2010

You can use Pascal scripting in InnoSetup to create new pages for the installer. These pages can be integrated into the normal installation flow. This is well documented within the InnoSetup documentation (Google search should also come up with samples). Also the Samples folder within your Program Files\InnoSetup has some code examples.

Some time ago, there was a software called InnoSetup Form designer, which allowed you to visually design the page. The link is still there, but on the page I could not find the download. Maybe if you look around a bit you can find it?

EDIT
This is a sample for a page I made once. This is the code section of the ISS file.[Code]

var
  EnableFolderPage: Boolean;
  lblBlobFileFolder: TLabel;
  lblBlobFileWarning1: TLabel;
  lblBlobFileWarning2: TLabel;
  tbBlobFileFolder: TEdit;
  btnBlobFileFolder: TButton;



function GetBlobFolder(param: String): String;
begin
  Result := Trim(tbBlobFileFolder.Text);
end;


{ BlobFileForm_Activate }
procedure BlobFileForm_Activate(Page: TWizardPage);
var
  s: string;
begin
  s := Trim(tbBlobFileFolder.Text);
  if (s = '') then
  begin
    tbBlobFileFolder.Text := ExpandConstant('{sys}');
  end;
end;


{ BlobFileForm_NextButtonClick }
function BlobFileForm_NextButtonClick(Page: TWizardPage): Boolean;
var
  s: string;
begin
  s := Trim(tbBlobFileFolder.Text);
  if (s = '') then
  begin
    MsgBox(ExpandConstant('{cm:BlobFileForm_NoFolder}'), mbError, MB_OK);
    Result := false;
  end else
  begin
    if not DirExists(s) then
    begin
      MsgBox(ExpandConstant('{cm:BlobFileForm_DirDoesntExist}'), mbError, MB_OK);
      Result := false;
    end else
    begin
      Result := True;
    end;
  end;
end;

procedure btnBlobFileFolder_Click(sender: TObject);
var
  directory: string;
begin
  if BrowseForFolder('', directory, true) then
  begin
    tbBlobFileFolder.Text := directory;
  end;
end;


{ BlobFileForm_CreatePage }
function BlobFileForm_CreatePage(PreviousPageId: Integer): Integer;
var
  Page: TWizardPage;
begin
  Page := CreateCustomPage(
    PreviousPageId,
    ExpandConstant('{cm:BlobFileForm_Caption}'),
    ExpandConstant('{cm:BlobFileForm_Description}')
  );

{ lblBlobFileFolder }
  lblBlobFileFolder := TLabel.Create(Page);
  with lblBlobFileFolder do
  begin
    Parent := Page.Surface;
    Caption := ExpandConstant('{cm:BlobFileForm_lblBlobFileFolder_Caption0}');
    Left := ScaleX(8);
    Top := ScaleY(8);
    Width := ScaleX(167);
    Height := ScaleY(13);
  end;

  { lblBlobFileWarning1 }
  lblBlobFileWarning1 := TLabel.Create(Page);
  with lblBlobFileWarning1 do
  begin
    Parent := Page.Surface;
    Caption := ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning1_Caption0}');
    Left := ScaleX(8);
    Top := ScaleY(80);
    Width := ScaleX(50);
    Height := ScaleY(13);
    Font.Color := -16777208;
    Font.Height := ScaleY(-11);
    Font.Name := 'Tahoma';
    Font.Style := [fsBold];
  end;

  { lblBlobFileWarning2 }
  lblBlobFileWarning2 := TLabel.Create(Page);
  with lblBlobFileWarning2 do
  begin
    Parent := Page.Surface;
    Caption :=
      ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption0}') + #13 +
      ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption1}') + #13 +
      ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption2}') + #13 +
      ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption3}') + #13 +
      ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption4}');
    Left := ScaleX(8);
    Top := ScaleY(96);
    Width := ScaleX(399);
    Height := ScaleY(133);
    AutoSize := False;
    WordWrap := True;
  end;

  { tbBlobFileFolder }
  tbBlobFileFolder := TEdit.Create(Page);
  with tbBlobFileFolder do
  begin
    Parent := Page.Surface;
    Left := ScaleX(8);
    Top := ScaleY(24);
    Width := ScaleX(401);
    Height := ScaleY(21);
    TabOrder := 0;
  end;

  { btnBlobFileFolder }
  btnBlobFileFolder := TButton.Create(Page);
  with btnBlobFileFolder do
  begin
    Parent := Page.Surface;
    Caption := ExpandConstant('{cm:BlobFileForm_btnBlobFileFolder_Caption0}');
    Left := ScaleX(320);
    Top := ScaleY(48);
    Width := ScaleX(91);
    Height := ScaleY(23);
    TabOrder := 1;
  end;

  with Page do
  begin
    OnActivate := @BlobFileForm_Activate;
    OnNextButtonClick := @BlobFileForm_NextButtonClick;
  end;

  with btnBlobFileFolder do
  begin
    OnClick := @btnBlobFileFolder_Click;
  end;

  Result := Page.ID;
end;


procedure InitializeWizard();
begin
  BlobFileForm_CreatePage(wpSelectDir);
end;

EDIT 2
To write the value the user entered to a registry key, create a new function:

function GetUserEnteredText(param: String): String;
begin
  Result := Trim(tbTextBox.Text);
end;

This function simply returns what was entered in the text box. Please note that the function must take a string parameter - even though you ignore it!

In the [Registry] section of your script, declare the key that should be written like that:

Root: HKLM; Subkey: SOFTWARE\MyCompany\MyTool; ValueType: string; ValueName: MyValue; ValueData: {code:GetUserEnteredText}; Flags: createvalueifdoesntexist uninsdeletekeyifempty uninsdeletevalue

This creates a registry value named "MyValue" in HKLM\SOFTWARE\MyCompany\MyTool that contains what the user entered in the text box.