3

I would like to install my setup content only to one specific directory, so I want to have the Next button on directory selection page disabled, unless the user chooses the right folder to install to.

How can I disable the Next button on directory selection page and enable it right after user chooses a specific directory ?

TLama
  • 75,147
  • 17
  • 214
  • 392
user1320880
  • 115
  • 1
  • 2
  • 5
  • 1
    Welcome to StackOverflow! Wouldn't be *the Next button is greyed until the user chooses the right folder* quite misleading ? What if I as the user forget the right directory ? Wouldn't be better to disable the *choose folder* edit box or skip that page at all ? – TLama Apr 09 '12 at 08:27
  • Since the installation directory isn't the same on every computer and the program doesn't have any registry keys to read the installation path from, it wouldn't be better. The user won't forget the right directory, since I have a readme file and the installer also has custom texts to aid the user. – user1320880 Apr 09 '12 at 08:29

2 Answers2

6

The following sample shows how to disable the Next button when you reach the SelectDir page and enable it only when you enter (or choose from the browse directory dialog) the C:\MySecretDir folder (the MySecretDir constant). The comparing is case insensitive since user can enter whatever he (or she) wants.

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
const
  MySecretDir = 'C:\MySecretDir';

procedure OnDirEditChange(Sender: TObject);
begin
  WizardForm.NextButton.Enabled := CompareText(WizardDirValue, MySecretDir) = 0;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = wpSelectDir then
    OnDirEditChange(nil);
end;

procedure InitializeWizard;
begin
  WizardForm.DirEdit.OnChange := @OnDirEditChange;
end;

Or if you want to enable the Next button only if there's a specific file MyUniqueFile.exe in the chosen directory, modify the code in OnDirEditChange event handler this way:

procedure OnDirEditChange(Sender: TObject);
begin
  WizardForm.NextButton.Enabled := FileExists(AddBackslash(WizardDirValue) +
    'MyUniqueFile.exe');
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 2
    What can I say? Perfect, just the thing I was looking for! Thank you very much! – user1320880 Apr 09 '12 at 12:52
  • 1
    It seems more user friendly to just do the check in NextButtonClick, throw a message box in case it fails, and make the function return false. This way you can notify the user _why_ he can't continue. Also, disabling the button probably won't have any effect on silent installs, whereas making NextButtonClick return false _does_. – Nyerguds Mar 21 '13 at 11:42
  • 1
    @Nyerguds, yes, might be; I just answered the asked question. About the silent setup, it won't have any effect as this question asks about user's selection. – TLama Mar 21 '13 at 11:49
  • 1
    @TLama Why would the LowerCase() function shown here not work for me if I'm running 5.5.5? – RichC Feb 18 '15 at 14:41
  • 1
    Actually, it works for me by itself but not inside a StringChangeEX() function for some reason. – RichC Feb 18 '15 at 14:45
  • @RichC, the `LowerCase` function works with ASCII character set (so it's limited), but without knowing any further details (like example input) it's not possible to help you. Better ask a new question. However, the `LowerCase` function is not good here, I'll replace that comparison with `CompareText`. – TLama Feb 18 '15 at 14:49
  • I just separated it outside of the StringChangeEX and it works now - thanks! – RichC Feb 18 '15 at 14:50
3

You could stop users from changing the install directory, by disabling the directory selection page. When disable the page will not be shown during the install.

[SETUP]
DisableDirPage=yes
Andrew Seaford
  • 645
  • 6
  • 13
  • That's right, however the OP's requirement was *I want to have the Next button on directory selection page disabled, unless the user chooses the right folder to install to.* – TLama Feb 24 '15 at 07:32