Search code examples
inno-setuppascalscript

Inno Setup: "Cannot focus disabled or invisible window" while setting ActiveControl property


I am trying to focus a button in a custom options window, which is shown modally. I have already read Prevent button from receiving focus in Inno Setup and tried the use the ActiveControl property to set this. Here is the code:

[Code]

{ Create and show the Options window }
procedure ShowOptionsWindow;
var
  OptionsOKButton, OptionsCancelButton: TButton;
begin
  OptionsWindowForm := TForm.Create(WizardForm);
  with OptionsWindowForm do
    begin
      Parent := WizardForm;
      BorderStyle := bsDialog;
      Position := poOwnerFormCenter;
      ClientWidth := ScaleX(425);
      ClientHeight := ScaleY(165);
      Caption := '{#AppName} Options';
    end;
{ Define the Options Cancel button }
  OptionsCancelButton := TButton.Create(OptionsWindowForm);
  with OptionsCancelButton do
    begin
      Parent := OptionsWindowForm;
      Left := (OptionsWindowForm.ClientWidth - WizardForm.NextButton.Width) - (WizardForm.ClientWidth - (WizardForm.CancelButton.Left + WizardForm.CancelButton.Width));
      Top := (OptionsWindowForm.ClientHeight - WizardForm.NextButton.Height) - ScaleY(12);
      Width := WizardForm.NextButton.Width;
      Height := WizardForm.NextButton.Height;
      Caption := 'Cancel';
      OnClick := @OptionsCancelButtonClick;
    end;
{ Define the Options OK button }
  OptionsOKButton := TButton.Create(OptionsWindowForm);
  with OptionsOKButton do
    begin
      Parent := OptionsWindowForm;
      Left := (OptionsCancelButton.Left - WizardForm.NextButton.Width) - ((WizardForm.CancelButton.Left - WizardForm.NextButton.Left) - WizardForm.NextButton.Width);
      Top := OptionsCancelButton.Top;
      Width := WizardForm.NextButton.Width;
      Height := WizardForm.NextButton.Height;
      Caption := 'OK';
      OnClick := @OptionsOKButtonClick;
    end;
  OptionsWindowForm.ActiveControl := OptionsOKButton;
  OptionsWindowForm.ShowModal;
end;

However, when running this, it gives the following error:

Runtime Error

I tried changing when this is called, by placing it after showing the window, but it doesn't get called until the window is closed, as ShowModal stops the scripts execution, when it still gives the same error. Is there a way to set a button to be focused in a modal window?


Solution

  • Your code is imo correct. It looks like a bug to me.

    It's the setting of the OptionsWindowForm.Parent property that triggers the problem. Just remove it.

    enter image description here