Gui submit seems not to submit...

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
LeSeb
Posts: 17
Joined: 25 Oct 2017, 14:50

Gui submit seems not to submit...

Post by LeSeb » 23 Jun 2021, 05:08

Hey,

I'm a newbie with V2 but comfortable with V1
Question : Why the checkboxes from this GUI does not return 1 after i checked them ?

Any idea ?

Thanks to those who can help me with this.

Code: Select all

softwares := [{name: "ccleaner", url: "https www.ccleaner.com /ccleaner/download/standard", ticked: 0}
,{name: "adwcleaner", url: "https downloads.malwarebytes.com /file/adwcleaner", ticked: 0}]

gestionnaire := Gui()
loop(softwares.Length)
{
    gestionnaire.add("Checkbox", softwares[a_index].ticked, softwares[a_index].name)
}
okbutton := gestionnaire.add("Button", "Default", "OK").OnEvent("Click", OK_click)
gestionnaire.show()

OK_click(*)
{
    gestionnaire.submit()
    gestionnaire.hide()
    loop(softwares.Length)
    {
        MsgBox(A_index . " - " . softwares[a_index].ticked) ; debug
        if ( softwares[A_Index].ticked = 1 )
            {
                run(softwares[A_Index].url)
                pause("toggle")
            }
    }
}


^p::pause("toggle")
^q::ExitApp
^!r::Reload


swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Gui submit seems not to submit...

Post by swagfag » 23 Jun 2021, 08:06

did u read how .Submit() works?
Collects the values from named controls and composes them into an Object. Optionally hides the window
NamedCtrlContents := MyGui.Submit([Hide := true])
not only are u not capturing the return value of the call to .Submit():

Code: Select all

OK_click(*)
{
    gestionnaire.submit()
; ^^ - ????
but even if u had, ud be getting an empty object back - u havent created any named controls to begin with!
a named control is one with vTypeTheNameHere in its options during creation
V: Sets the control's Name. Specify the name immediately after the letter V, which is not included in the name. For example, specifying vMyEdit would name the control "MyEdit".
or a created one whose .Name property has been assigned to
Retrieves or sets the name of the control.
GuiCtrl.Name := NewName
so far all uve done is pass 0 to each checkbox's options(which tries to set a window style corresponding to that number, which effectively doesnt do anything in this case)

Code: Select all

#Requires AutoHotkey v2.0-a137-f6f65742

softwares := [{name: "ccleaner", url: "https www.ccleaner.com /ccleaner/download/standard", ticked: 0}
,{name: "adwcleaner", url: "https downloads.malwarebytes.com /file/adwcleaner", ticked: 0}]

gestionnaire := Gui()

for SW in softwares
    gestionnaire.add("Checkbox", 'Checked' SW.ticked ' v' SW.name, SW.name)
;                                                      ^^^^^^^^^^ - make it a named control
    ; gestionnaire.add("Checkbox", 'Checked' SW.ticked, SW.name).Name := SW.Name ; alternative way

okbutton := gestionnaire.add("Button", "Default", "OK")
okbutton.OnEvent("Click", OK_click) ; OnEvent void return type

gestionnaire.show()
gestionnaire.OnEvent('Close', (*) => ExitApp())

OK_click(*)
{
    NamedCtrlContents := gestionnaire.submit()
    ; gestionnaire.hide()

    for SW in softwares
        if NamedCtrlContents.%SW.name% ; equivalent to, eg: isChecked := NamedCtrlContents['ccleaner']
            MsgBox SW.url
}

different way without using .Submit()

Code: Select all

#Requires AutoHotkey v2.0-a137-f6f65742

gestionnaire := Gui()

softwares := [{name: "ccleaner", url: "https www.ccleaner.com /ccleaner/download/standard", ticked: 0}
,{name: "adwcleaner", url: "https downloads.malwarebytes.com /file/adwcleaner", ticked: 0}]

Checkboxes := []
for SW in softwares
{
    CB := gestionnaire.add("Checkbox", 'Checked' SW.ticked, SW.name)
    CB.url := SW.url ; attach this value property as extra data to be carried around and used later
    CB.DefineProp('ticked', Gui.Control.Prototype.GetOwnPropDesc('Value')) ; alias CB.ticked to CB.Value
    CB.DefineProp('name', Gui.Control.Prototype.GetOwnPropDesc('Text')) ; alias CB.name to CB.Text
        
    Checkboxes.Push(CB)
}

okbutton := gestionnaire.add("Button", "Default", "OK")
okbutton.OnEvent("Click", OK_click) ; OnEvent void return type

gestionnaire.show()
gestionnaire.OnEvent('Close', (*) => ExitApp())

OK_click(*)
{
    ; gestionnaire.hide()

    for CB in Checkboxes
        if CB.ticked
            MsgBox CB.url
}

.OnEvent() doesnt return anything, so writing:

Code: Select all

okbutton := gestionnaire.add("Button", "Default", "OK").OnEvent("Click", OK_click)
and expecting an object of type Gui.Control.Button to be stored in okbutton is an error. the variable is blank

LeSeb
Posts: 17
Joined: 25 Oct 2017, 14:50

Re: Gui submit seems not to submit...

Post by LeSeb » 23 Jun 2021, 09:06

Thank you for this detailed answer and explanations.

Indeed, i didn't read the submit method doc...i made a stupid mistake here imagining that V2 would acts like V1 on this point.
I still have a lot to learn but learnt a lot with your answer, thank you very much !

Post Reply

Return to “Ask for Help (v2)”