Question

I'm using the following AHK script to pop up screen with checkboxes. Idea is that when I check on the boxes it should open a folder. It works for the first one, but when I check the second one nothing happens. Any ideas whats wrong with my code

Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open 


Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return

ButtonGo:
Gui, Submit
Gui, Destroy


If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return

If Open2 = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"
Return


GUIClose:
Gui, Destroy
Was it helpful?

Solution

When you use an if statement without curly brackets, only the following line is read by the if statement.

That means that when you do this:

If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return

...the program ends. The if statement does the run command and the return gets processed no matter what.

So, try this, instead, if you want multiple lines:

If(Open = 1){
    Run "X:\SSC_HR\SENS\Bedrijfsbureau"
    Return
}

Also, nothing will happen anyway! First, when you check the box, you have no sub to handle the checkbox. In other words, when you click the checkbox, nothing is supposed to happen. So, I suppose that you only want things to happen when you push the "Go" button.

That's great, but your go button does two things: it grabs the value of the checkbox, then it exits the GUI ----> THIS IS WHY YOUR PROGRAM DOESN'T WORK.

You want to do this, instead:

Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open 


Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return

ButtonGo:
    ;fetch the value of the checkbox variables
    Gui, Submit 

    If Open = 1
    Run "X:\SSC_HR\SENS\Bedrijfsbureau"

    If Open2 = 1
    Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"

    ;Now, if you want the gui to close after the actions are complete, then uncomment the next line - if you want the gui to remain, then don't uncomment it.
    ;Gui, Destroy
RETURN

GUIClose:
Gui, Destroy

Finally, in this scenario, you can execute either one or both of the checkbox commands. If you only want one or the other, you should use radio buttons instead.

For a hotkey to run the primary function:

#x::
gosub, ButtonGo
return

If you want the hotkey to open the GUI, then you need to wrap the GUI in a gosub and alter the hotkey's command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top