Search code examples
installationwindows-installernsis

Making checkboxes behave like radio buttons, but one of the sections is selected by default - NSIS Components Page


I am looking for a way so certain components behave like radio buttons, but one of the additional components is selected by default - right after starting the installer. Unfortunately the main component gets also unselected when choosing other components, and the built-in function .onSelChange ignores the initially selected component.

NSIS Components Page with radio selection

!include "MUI2.nsh"
!include "Sections.nsh"
OutFile "ABC.exe"
Name "ABC 1.0"

!insertmacro MUI_PAGE_COMPONENTS

Section "!ABC main" SEC_main
SectionEnd

Section /o "ABC expansion 1" SEC_exp1
SectionEnd

Section /o "ABC expansion 2" SEC_exp2
SectionEnd

Section /o "ABC expansion 3" SEC_exp3
SectionEnd

Section /o "ABC expansion 4" SEC_exp4
SectionEnd

Section /o "ABC expansion 5" SEC_exp5
SectionEnd

 Function .onSelChange
  !insertmacro StartRadioButtons $1
      !insertmacro RadioButton ${SEC_exp1}
      !insertmacro RadioButton ${SEC_exp2}
      !insertmacro RadioButton ${SEC_exp3}
      !insertmacro RadioButton ${SEC_exp4}
      !insertmacro RadioButton ${SEC_exp5}
  !insertmacro EndRadioButtons
 FunctionEnd

 Function .onInit
  SectionSetFlags ${SEC_main} 25
  SectionSetFlags ${SEC_exp3} 1
FunctionEnd

Solution

  • The solution is to create a variable (in the code below it's $abc_sec_exp_group) that stores the information of the initially selected component.

    var abc_sec_exp_group
    
    ; ...
    
     Function .onSelChange
      !insertmacro StartRadioButtons $abc_sec_exp_group
          !insertmacro RadioButton ${SEC_exp1}
          !insertmacro RadioButton ${SEC_exp2}
          !insertmacro RadioButton ${SEC_exp3}
          !insertmacro RadioButton ${SEC_exp4}
          !insertmacro RadioButton ${SEC_exp5}
      !insertmacro EndRadioButtons
     FunctionEnd
    
     Function .onInit
      SectionSetFlags ${SEC_main} 25
      SectionSetFlags ${SEC_exp3} 1
      StrCpy $abc_sec_exp_group ${SEC_exp3} ;<-------;
    FunctionEnd