You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用IsComponentSelected函数控制自定义页面项启用/禁用的代码问题

Fix: Real-time Enable/Disable Custom Page Controls based on Component Selection in Inno Setup

I've run into this exact issue before! The problem with your original code is that you're only updating the control state when the custom page is displayed. When you uncheck a component and click Next immediately, the custom page hasn't loaded yet—so the state doesn't refresh until you navigate back and forth.

Here's the corrected code that ensures the control state updates in real-time as you change component selections, so the first time you click Next, the control is already enabled/disabled correctly:

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

[Types]
Name: full; Description: "Full Installation"
Name: custom; Description: "Custom Installation"

[Components]
Name: main; Description: "Main Program"; Types: full custom; Flags: fixed
Name: extra; Description: "Extra Features"; Types: full custom

[Code]
var
  CustomPage: TWizardPage;
  ExtraDirEdit: TEdit; // Your custom directory input control

// Core function to update control state based on component selection
procedure UpdateCustomPageControls;
begin
  // Replace 'extra' with your target component name
  ExtraDirEdit.Enabled := IsComponentSelected('extra');
end;

// Trigger update whenever the components list is clicked (selection changes)
procedure ComponentsListClick(Sender: TObject);
begin
  UpdateCustomPageControls;
end;

procedure InitializeWizard;
begin
  // Create your custom page
  CustomPage := CreateCustomPage(
    wpSelectComponents, 
    'Extra Features Settings', 
    'Specify the directory for extra features'
  );

  // Add your directory edit control (adjust positioning as needed)
  ExtraDirEdit := TEdit.Create(WizardForm);
  ExtraDirEdit.Parent := CustomPage.Surface;
  ExtraDirEdit.Left := 16;
  ExtraDirEdit.Top := 40;
  ExtraDirEdit.Width := WizardForm.DirEdit.Width;
  ExtraDirEdit.Text := ExpandConstant('{pf}\My Program\Extra');
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = wpSelectComponents then
  begin
    // Bind the click event to the components list when entering the page
    WizardForm.ComponentsList.OnClick := @ComponentsListClick;
    // Set initial state when loading the components page
    UpdateCustomPageControls;
  end
  else if CurPageID = CustomPage.ID then
  begin
    // Double-check state when the custom page becomes active
    UpdateCustomPageControls;
  end;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;
  if CurPageID = wpSelectComponents then
  begin
    // Final state update before navigating to the custom page
    UpdateCustomPageControls;
  end;
end;

Key Fixes Explained:

  • Real-time Update on Component Selection: The ComponentsListClick event triggers UpdateCustomPageControls every time the user clicks a component, so the control state updates immediately instead of waiting for page navigation.
  • Multiple Update Checkpoints: We update the state in three places to cover all scenarios:
    • When entering the components page (initial load)
    • When the custom page is displayed
    • Right before leaving the components page via the Next button
  • Direct Control Reference: We store a reference to the custom directory control so we can modify its state directly from anywhere in the code.

This ensures that no matter how the user interacts with the components page (clicking with the mouse, using keyboard shortcuts), the custom page controls will always reflect the current component selection when they navigate to it.

内容的提问来源于stack exchange,提问作者Techie Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:45:27