0

Good day,

I'm trying to create a script for AutoHotKey (AHK). The script itself already works fine: a small GUI with two radio buttons for each color (black or white), but I want to have a single function and not a gosub for each color as it is now, because I want in the future to add much more colors.

Here is the GUI:

enter image description here

Here is the script:

Gui, Main:new

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, Button, gEnter xp-11 yp+42, Enter

Gui, Show
return

Enter:
    Gui, Submit, NoHide
    Switch {
        Case Color1:
            gosub ColorBlack
        Case Color2:
            gosub ColorWhite
    }
    Gui, Hide
return

GuiClose:
ExitApp

ColorBlack:
    Gui, ColorBlack: Add, Picture,, %A_ScriptDir%\color-black.png
    Gui, ColorBlack: Show
return

ColorWhite:
    Gui, ColorWhite: Add, Picture,, %A_ScriptDir%\color-white.png
    Gui, ColorWhite: Show
return

That's also why I need to keep this structure as it is now. Therefore, in order to have only one function (only Color instead of ColorBlack and ColorWhite) I would need to reference the certain part of the path to the radio button choice (so that it puts the color name in the right spot):

Color:
    Gui, ColorBlack: Add, Picture, x0 y0 w1920 h1920, %A_ScriptDir%\color-%color%.png
    Gui, ColorBlack: Show
return

The above one of course doesn't work – has anybody an idea how to solve this?

1 Answer 1

1
Gui, Main:new
Gui, Add, GroupBox, x10 y6 w150 h100 R3, Color:
Gui, Add, Radio, Checked        vColor xp+10 yp+20  ,   Black   ; 1 -   you may use just one var for a radio button group, it'll be indexed
Gui, Add, Radio,                                    ,   White   ; 2
Gui, Add, Radio,                                    ,   Yellow  ; 3 and so on

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

Gui, Show

return

; Enter:
    ; Gui, Submit, NoHide
    ; Switch {

        ; Case Color1:  ;   just so you know, you dont need to use gosub with switch/case, you may just nest it under each case, but this would'nt be the best choice in this situtation
            ; Gui, ColorBlack: Add, Picture,, %A_ScriptDir%\color-black.png
            ; Gui, ColorBlack: Show

        ; Case Color2:
            ; Gui, ColorWhite: Add, Picture,, %A_ScriptDir%\color-white.png
            ; Gui, ColorWhite: Show

    ; }
    ; Gui, Hide
; return

GuiClose:
    ExitApp


Enter:

    Gui, Submit

    selected_color :=   color = 1 ; ternary operator to determine which color is selected | if ? then : else
                            ?   "black"
                    :   color = 2
                            ?   "white"
                    :           "yellow"
    Gui, Color:Add, Picture,,% A_ScriptDir "\color-" selected_color ".png"
    Gui, Color:Show


return

ColorGuiClose:  ;   destroy the color gui and show the main gui

    Gui, Color:Destroy
    Gui, Main:Show

Return
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.