GUI Control inside function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
L40P
Posts: 19
Joined: 05 Jul 2016, 15:49

GUI Control inside function

22 Jul 2017, 12:56

Hi Community,
I once again have a problem. I have a function in which I create a GUI with a text to display a 5sec timer. But it says "A control's variable must be global or static".

Code: Select all

GuiShow() 
{
	Gui, SleepWarning:Add, Text, vCountdown, 5
	Gui, SleepWarning:Show, Center, SleepWarning
	Sleep, 1000
	GuiControl, , Countdown, 4
	Sleep, 1000
	GuiControl, , Countdown, 3
	Sleep, 1000
	GuiControl, , Countdown, 2
	Sleep, 1000
	GuiControl, , Countdown, 1
	Sleep, 1000
	GuiControl, , Countdown, 0
	Sleep, 250
	...
}
How can I edit the control without that error or how can I declare that control's variable as global?
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: GUI Control inside function

22 Jul 2017, 13:03

Hi.
You can create your GUI outside the function and can use only Gui, SleepWarning: show ... inside your function to display your sleep warning.
I think this will prevent your error message and the need of using definitions for the variables.
Einfach nur ein toller Typ. :mrgreen:
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: GUI Control inside function  Topic is solved

22 Jul 2017, 13:08

Code: Select all

GuiShow()
ExitApp

GuiShow() 
{
    static Countdown
    
	Gui, SleepWarning:Add, Text, vCountdown, 5
	Gui, SleepWarning:Show, Center, SleepWarning
	Sleep, 1000
	GuiControl, SleepWarning:, Countdown, 4
	Sleep, 1000
	GuiControl, SleepWarning:, Countdown, 3
	Sleep, 1000
	GuiControl, SleepWarning:, Countdown, 2
	Sleep, 1000
	GuiControl, SleepWarning:, Countdown, 1
	Sleep, 1000
	GuiControl, SleepWarning:, Countdown, 0
	Sleep, 250
}
User avatar
L40P
Posts: 19
Joined: 05 Jul 2016, 15:49

Re: GUI Control inside function

22 Jul 2017, 13:13

Code: Select all

Gui, SleepWarning:Add, Text, vCountdown, 5

GuiShow() 
{
	Gui, SleepWarning:Show, Center, SleepWarning
	Sleep, 1000
	GuiControl, , Countdown, 4
	Sleep, 1000
	GuiControl, , Countdown, 3
	Sleep, 1000
	GuiControl, , Countdown, 2
	Sleep, 1000
	GuiControl, , Countdown, 1
	Sleep, 1000
	GuiControl, , Countdown, 0
	Sleep, 250
	...
}
I think you mean like this? The error message doesn't show up anymore that's right, but now the text won't update. The timer and script afterwards runs but the text doesn't change.
User avatar
L40P
Posts: 19
Joined: 05 Jul 2016, 15:49

Re: GUI Control inside function

22 Jul 2017, 13:21

Thanks kon. Your code works great :)
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: GUI Control inside function

22 Jul 2017, 13:30

There is a problem. The Gui is not destroyed, so when you call the function a second time it will give you a warning about not being able to use the same variable for more than 1 control.
You can try this. Then try it without the line that destroys the Gui.

Code: Select all

GuiShowSleepWarning()
GuiShowSleepWarning()
ExitApp

GuiShowSleepWarning() 
{
    static Countdown
    
    Countdown := 5
	Gui, SleepWarning:Add, Text, vCountdown, % Countdown
	Gui, SleepWarning:Show, Center, SleepWarning
    Loop {
        Sleep, 1000
        GuiControl, SleepWarning:, Countdown, % --Countdown
        if (Countdown < 1) {
            Sleep, 250
            Gui, SleepWarning:Destroy
            return
        }
    }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: zerox and 311 guests