Why is this gui not closing ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Why is this gui not closing ?

Post by WalkerOfTheDay » 31 Mar 2023, 06:55

Hi. When I click the X to close my gui, it works perfectly. But when I press my guibutton that leads to ResetGui(*), it executes the
messagebox just fine, and when I press No, it asks me if I want to close the gui. But when I press the Yes button the gui doesn't close.
Why is that ?

Snippet:

Code: Select all

ResetGui(*)
{
    if MsgBox("Do you want to practice another multiplication ?",, "y/n 4096") = "Yes"
    {
        Loop 10
            TableButton[A_Index].Enabled := true ; enable all buttons
        global counter          := 1
        global mytable          := 1
        global mistakes         := 0
        AnswerBtn.visible       := 0
        AddCtrl.visible         := 0
        AnswerText.visible      := 0
        EditCtrl.visible        := 0
        EditCtrl.Text           := ""
        NoOfMistakes.visible    := 0
        return
    }
    else
       Close_Gui()
    return
    
}

Close_Gui(*)
{
    if MsgBox("Are you sure you want to close the GUI?",, "y/n" . " " . "default2 4096") = "No" ; 'default2' makes the No button the default one
        return true  ; true = 1
}

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Why is this gui not closing ?

Post by mikeyww » 31 Mar 2023, 07:01

Hello,

I recommend posting a script that the forum reader can run to demonstrate the problem that you are experiencing.

Which line in your script closes the GUI?

User avatar
boiler
Posts: 16959
Joined: 21 Dec 2014, 02:44

Re: Why is this gui not closing ?

Post by boiler » 31 Mar 2023, 07:04

Do you expect AHK to know that you want to close the GUI you named the function Close_Gui()? And AHK should also know that's your intent when the user selects "Yes" because of the wording you put in the MsgBox?

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Why is this gui not closing ?

Post by WalkerOfTheDay » 31 Mar 2023, 07:07

@mikeyww: please find the full script attached

Code: Select all

#Requires AutoHotkey v2.0
counter     := 1
mytable     := 1
mistakes    := 0
AHKV2Gui := Gui("AlwaysOnTop", "My First AHKv2 Gui")
Ahkv2Gui.Add("Text",, "Which multiplication do you want to practice ?")
Ahkv2Gui.Add("Text",, "")
AHKv2Gui.Show()

TableButton := []
Loop 10
    {
        TableButton.Push(AHKV2Gui.Add("Button", "w40 yp", A_Index))
        TableButton[A_Index].OnEvent("Click", TableButton1_Click)
    }

AddCtrl             := Ahkv2Gui.Add("Text", "x10 y+30 w90 h13 hidden", counter . " times " . mytable " is ?")
AddCtrl.SetFont("s11 bold")
EditCtrl            := Ahkv2Gui.AddEdit("vAnswer center Number w40 yp-2 xp+95 hidden")
EditCtrl.SetFont("s11 bold")
AnswerBtn           := AHKv2Gui.Add("Button", "default yp hidden" , "Go")
AnswerBtn.OnEvent("Click", ClickAnswer)
AnswerText          := AHKV2Gui.Add("Text", "x0 yp+25 w500 center h65 hidden", "OK !")
AnswerText.SetFont("s40 bold")
NoOfMistakes        := AHKV2Gui.Add("Text", "y65 x315 h25  w250 hidden", "You made " . mistakes . " mistake")
NoOfMistakes.SetFont("s12 cff008c")
ResetBtn            := AHKV2Gui.Add("Button", "x420 y270", "Reset")
ResetBtn.OnEvent("Click", ResetGui)
Ahkv2Gui.OnEvent("Close", Close_Gui)
AHKV2Gui.Show("w500 h300 center")
return

TableButton1_Click(btnObj, *) {             ; btnObj is used to get the text from the pressed button
    global mytable := btnObj.Text
    Loop 10
        {
            TableButton[A_Index].Enabled := false ; disable all buttons until finished
        }
    counter             := 1
    AddCtrl.Text        := counter " times " mytable " is: "
    AddCtrl.Visible     := 1
    EditCtrl.Visible    := 1
    EditCtrl.Focus
    AnswerBtn.Visible   := 1
    return
}

ClickAnswer(*) {
    global mistakes
    Saved := AHKV2Gui.Submit(0)
    if (Saved.Answer = "")
             return
    if (Saved.Answer = counter * mytable) ; means the answer is correct
        {
            global counter
            if (counter = 10)
                {
                    ResetGui()       
                    return
                }
                
            ++counter               ; used to be counter++ in V1
            AddCtrl.Text        := counter " times " mytable " is: "
            EditCtrl.Text       := ""
            EditCtrl.Focus
            AnswerText.Text     := "OK !"
            AnswerText.SetFont("cGreen")
            AnswerText.visible  := 1
            Sleep 1000
            EditCtrl.Focus
            AnswerText.visible  := 0     
         }
    else
        {
            ++mistakes
            NoOfMistakes.Visible    := 1
            if (Mistakes = 1)
                NoOfMistakes.Text   := "You made " . mistakes . " mistake"
            else
                NoOfMistakes.Text   := "You made " . mistakes . " mistake(s)"
            AnswerText.Text         := "Try again"
            AnswerText.SetFont("cRed")
            AnswerText.visible      := 1     
            Sleep 1000
            AnswerText.visible      := 0    
            EditCtrl.Text           := ""
            EditCtrl.Focus
            AnswerText.visible      := 0     
        }
    return
}

ResetGui(*)
{
    if MsgBox("Do you want to practice another multiplication ?",, "y/n 4096") = "Yes"
    {
        Loop 10
            TableButton[A_Index].Enabled := true ; enable all buttons
        global counter          := 1
        global mytable          := 1
        global mistakes         := 0
        AnswerBtn.visible       := 0
        AddCtrl.visible         := 0
        AnswerText.visible      := 0
        EditCtrl.visible        := 0
        EditCtrl.Text           := ""
        NoOfMistakes.visible    := 0
        return
    }
    else
       Close_Gui   ; >>>>> Replacing this with ExitApp works, but I'm wondering why the function call the function does not work ??
    return
    
}

Close_Gui(*)
{
    if MsgBox("Are you sure you want to close the GUI?",, "y/n" . " " . "default2 4096") = "No" ; 'default2' makes the No button the default one
        return true  ; true = 1
}

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is this gui not closing ?  Topic is solved

Post by just me » 31 Mar 2023, 07:16

The function does not close the Gui window, it will be called internally when the window is going to be closed:

Code: Select all

ResetGui(*)
{
    if MsgBox("Do you want to practice another multiplication ?",, "y/n 4096") = "Yes"
    {
        Loop 10
            TableButton[A_Index].Enabled := true ; enable all buttons
        global counter          := 1
        global mytable          := 1
        global mistakes         := 0
        AnswerBtn.visible       := 0
        AddCtrl.visible         := 0
        AnswerText.visible      := 0
        EditCtrl.visible        := 0
        EditCtrl.Text           := ""
        NoOfMistakes.visible    := 0
        return
    }
    else
       WinClose(AHKV2Gui.Hwnd) ; Close_Gui   ; >>>>> Replacing this with ExitApp works, but I'm wondering why call the function does not work ??
    return
    
}

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Why is this gui not closing ?

Post by WalkerOfTheDay » 31 Mar 2023, 07:23

just me wrote:
31 Mar 2023, 07:16
The function does not close the Gui window, it will be called internally when the window is going to be closed:

Code: Select all

ResetGui(*)
{
    if MsgBox("Do you want to practice another multiplication ?",, "y/n 4096") = "Yes"
    {
        Loop 10
            TableButton[A_Index].Enabled := true ; enable all buttons
        global counter          := 1
        global mytable          := 1
        global mistakes         := 0
        AnswerBtn.visible       := 0
        AddCtrl.visible         := 0
        AnswerText.visible      := 0
        EditCtrl.visible        := 0
        EditCtrl.Text           := ""
        NoOfMistakes.visible    := 0
        return
    }
    else
       WinClose(AHKV2Gui.Hwnd) ; Close_Gui   ; >>>>> Replacing this with ExitApp works, but I'm wondering why call the function does not work ??
    return
    
}
Hi @just me

Thanks for your answer, but why does the function work correctly when I press the close (X) button in the upper right corner ?

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Why is this gui not closing ?

Post by just me » 31 Mar 2023, 07:35

Because of

Code: Select all

Ahkv2Gui.OnEvent("Close", Close_Gui)
If you press the close button, it will trigger the event Close and therefore call the function Close_Gui. All the function is able to do is to prevent closing by returning 1, but only if it has been called internally. It does not close the window by itself.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Why is this gui not closing ?

Post by WalkerOfTheDay » 31 Mar 2023, 07:49

Okay clear. Thank you !

Post Reply

Return to “Ask for Help (v2)”