function doesn't seem to reference a global GUI object variable. Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
alawsareps
Posts: 26
Joined: 20 Jul 2016, 09:28

function doesn't seem to reference a global GUI object variable.

08 May 2024, 04:18

This code behaves undesirably.

I intend to destroy the GUI, create a new one, and show it.

However, instead of that, it doesn't destroy the GUI; it keeps showing another GUI on top of another if I keep pressing F1.

Code: Select all

#Requires AutoHotkey v2.0

mode_GUI := Gui()
mode := "morning"

create_GUI(name_GUI, text) {
    name_GUI.destroy()
    name_GUI := GUI()
    name_GUI.Add("Text",, text)
    name_GUI.show()
}

f1:: {
    global mode
    mode == "morning" ? mode := "good evening" : mode := "morning"
    create_GUI(mode_GUI, mode)
return
}

Code below is what I'm trying to achieve, but I don't like to hardcode GUI variable. I want to be able to pass other GUI variable.

Code: Select all



#Requires AutoHotkey v2.0

mode_GUI := Gui()
mode := "morning"

create_GUI(text) {
    global mode_GUI

    mode_GUI.destroy()
    mode_GUI := GUI()
    mode_GUI.Add("Text",, text)
    mode_GUI.show()
}

f1:: {
    global mode
    mode == "morning" ? mode := "good evening" : mode := "morning"
    create_GUI(mode)
return
}

User avatar
WarlordAkamu67
Posts: 224
Joined: 21 Mar 2023, 06:52

Re: function doesn't seem to reference a global GUI object variable.  Topic is solved

08 May 2024, 05:11

Your function is creating another GUI that can not be referred to later on.
I have added two lines and altered one.
1: return(name_GUI) <-- this line returns the newly created gui
2. global mode_GUI <-- allows the f1 function to use the global variable. this can be reduced to just "global" to use both variables at once, but includes ALL global variables.

Altercation:
create_GUI(mode_GUI, mode) ----------> mode_GUI := create_GUI(mode_GUI, mode)

This sets the mode_GUI, which will be passed again on the next iteration of the function, to the returned value of our create_GUI function. This value is name_GUI.

Code: Select all

#Requires AutoHotkey v2.0

mode_GUI := Gui()
mode := "morning"

create_GUI(name_GUI, text) {
    name_GUI.Destroy()
    name_GUI := GUI()
    name_GUI.Add("Text",, text)
    name_GUI.Show()
    return(name_GUI)
}

f1:: {
    global mode_GUI
    global mode
    mode == "morning" ? mode := "good evening" : mode := "morning"
    mode_GUI := create_GUI(mode_GUI, mode)
    return
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Nandiman, Noitalommi_2, robinson and 22 guests