Search code examples
inno-setuppascalscript

Inno Setup Create dependency between custom check boxes


I have a custom page SelectPage that I am creating two custom check boxes on. The relevant parts of the code from InitializeWizard procedure are below:

//Define the Restart Services Checkbox
  RestartServicesCheckBox := TNewCheckBox.Create(WizardForm);
  with RestartServicesCheckBox do
    begin
      Parent := SelectPage.Surface;
      Caption := 'Restart Services';
      Left := OptionsLabel.Left;
      Top := OptionsLabel.Top + ScaleY(20);
      Checked := True;
    end;
//Define the Restart Server Checkbox
  RestartServerCheckBox := TNewCheckBox.Create(WizardForm);
  with RestartServerCheckBox do
    begin
      Parent := SelectPage.Surface;
      Caption := 'Restart Server';
      Left := OptionsLabel.Left;
      Top := RestartServicesCheckBox.Top + ScaleY(22);
      Checked := False
    end;

This works and I get the check boxes that I want and they perform the actions that I assign to them. What I am struggling to work out is how to assign a dependency between the two, so that if one is checked, the other is automatically unchecked. However, I do not want a radio buttons type dependency, as both checkboxes might need to be unchecked. I was looking at trying to intercept the OnClick event something like this:

var
  DefaultOnClick: TNotifyEvent;

procedure InitializeWizard();
begin
//Store the original OnClick event procedure and assign custom procedure
  DefaultOnClick := WizardForm.TCheckBox.OnClick;
  WizardForm.TCheckBox.OnClick := @OnClick;
end;

//Uncheck and Restart Services if Restart Server is checked and vice versa
procedure UpdateOptions();
begin
  with RestartServicesCheckBox do
    begin
      if RestartServicesCheckBox.Checked then
        begin
          Checked := False;
        end;
    end;
  with RestartServerCheckBox do
    begin
      if RestartServerCheckBox.Checked then
        begin
          Checked := False;
        end;
    end;
end;

//Update the options check boxes if the states change and restore the original event handler procedures
procedure OnClick(Sender: TObject);
begin
  DefaultOnClick(Sender);
  UpdateOptions;
end;

However, I do not know what the full event name is that I need to intercept. It clearly isn't WizardForm.TCheckBox.OnClick anyway. What would this event name be and will this method work? Alternatively, is there a simpler way to do this?


Solution

  • It is the TCheckBox.OnClick event that you need to use.

    var
      RestartServicesCheckBox: TNewCheckBox;
      RestartServerCheckBox: TNewCheckBox;
    
    procedure RestartServicesCheckBoxClick(Sender: TObject);
    begin
      if RestartServicesCheckBox.Checked then
        RestartServerCheckBox.Checked := False;
    end;
    
    procedure RestartServerCheckBoxClick(Sender: TObject);
    begin
      if RestartServerCheckBox.Checked then
        RestartServicesCheckBox.Checked := False;
    end;
    
    procedure InitializeWizard();
    begin
      // Define the Restart Services Checkbox
      RestartServicesCheckBox := TNewCheckBox.Create(WizardForm);
      with RestartServicesCheckBox do
      begin
        ...
        Checked := True;
        OnClick := @RestartServicesCheckBoxClick;
      end;
    
      // Define the Restart Server Checkbox
      RestartServerCheckBox := TNewCheckBox.Create(WizardForm);
      with RestartServerCheckBox do
      begin
        ...
        Checked := False;
        OnClick := @RestartServerCheckBoxClick;
      end;
    end;
    

    Though I think that three radio buttons might be better:

    • Do not restart
    • Restart service only
    • Restart server