Search code examples
checkboxradio-buttonautohotkey

AHK prevent radio button from executing action


Good day,

I'm trying to create a script for AutoHotKey (AHK). The script itself already works fine, but my issue is, that in the GUI when I press a radio button or checkbox ('Color' and 'Option' GroupBox) it automatically executes the action from the function (in this case "Combo"), but I want only to select the options and only toggle the function by pressing the "Enter" button on the bottom.

Here is the GUI:

enter image description here

Here is the script:

#SingleInstance Force

Gui, Main:new

Gui, Add, GroupBox, x10 y6 w150 h100 R3, Color:
Gui, Add, Radio, Checked        vColor1     gCombo xp+10 yp+20,     Black
Gui, Add, Radio,                vColor2     gCombo,                 White
 
Gui, Add, GroupBox, x10 w150 h100 R3, Option:
Gui, Add, CheckBox,             vOption2   gOption2 xp+10 yp+20,  Option 2
Gui, Add, CheckBox, Checked     vOption1   gOption1,              Option 1

Gui, Add, Button, gEnter xp-11 yp+42, Enter

Gui, Show
return

Enter:
    gosub Combo
    Gui, Hide
return

Combo:
    Gui, Submit, NoHide
    if ((Color1=1)&(Option2=0)&(Option1=0)){
        MsgBox, PLEASE CHOOSE OPTION
    }
    else if ((Color1=1)&(Option2=1)&(Option1=0)){
        gosub Option2
    }
    else if ((Color1=1)&(Option2=0)&(Option1=1)){
        gosub Option1
    }
return

GuiClose:
    Gui, Main:Destroy
ExitApp

Option2:
    MsgBox, You have chosen Option 2
return

Option1:
    MsgBox, You have chosen Option 1
return

The solution is probably not that complicated but I have no idea how to solve it.


Solution

  • This should help you:

    #SingleInstance Force
    
    Gui, Add, GroupBox , x10 y6 w150 h100 R3, Color:
    Gui, Add, Radio, Checked        vColor1   xp+10 yp+20,   Black
    Gui, Add, Radio,                vColor2  ,               White
     
    Gui, Add, GroupBox , x10 w150 h100 R3, Option:
    Gui, Add, CheckBox,          vOption2   xp+10 yp+20,  Option 2
    Gui, Add, CheckBox , Checked     vOption1   ,             Option 1
    
    Gui, Add, Button, gEnter xp-11 yp+42, Enter
    
    Gui, Show
    return
    
    Enter:
    
        Gui, Submit, NoHide
    
        Switch  {
    
            Case !option1 && !option2:
                MsgBox, PLEASE CHOOSE OPTION
    
            Case Color1 && Option2 && !Option1:
                MsgBox, Color 1 and option 2 selected
    
            Case Color1 && !Option2 && Option1:
                MsgBox, Color 1 and option 1 selected
    
            Case Color1 && Option2 && Option1:
                MsgBox, Color 1, option 1 and option 2 selected
    
            Case Color2 && Option2 && !Option1:
                MsgBox, Color 2 and option 2 selected
    
            Case Color2 && !Option2 && Option1:
                MsgBox, Color 2 and option 1 selected
    
            Case Color2 && Option2 && Option1:
                MsgBox, Color 2, option 1 and option 2 selected
    
        }
    
    return
    
    GuiClose:
        ExitApp