Unable to Destroy/Hide a Gui, Gui.Destroy() causes Error Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Unable to Destroy/Hide a Gui, Gui.Destroy() causes Error

23 Dec 2023, 00:06

My CapsLock is usually disabled. I wrote a script to enable it temporarily for 10 seconds with a notification Gui and then disable it. It also includes a Keywait that allows me to skip the 10s wait and disable it immediately, but the notification gui persists for the full 10 seconds.

Any attempt to Gui.Destroy fails, causes an error.
I tried MyNotification.Hide() but that causes the same error.
I have clearly misunderstood something but reading the docs hasn't helped.

Code: Select all

+CapsLock:: {
SetCapsLockState "On"
MyNotification("CapsLock ON", "10000", "960", "985", "1")   ; duration 10s
KeyWait "Esc", "d t10" ; esc skips 10s wait and disables CapsLock immediately
SetCapsLockState "Off"
MyNotification.Destroy() ; error
}

MyNotification(mytext, myduration, xAxis, yAxis, timer) {
MyNotification := Gui()
MyNotification.Opt("+AlwaysOnTop -Caption +ToolWindow")  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
MyNotification.BackColor := "EEEEEE"  ; White background
MyNotification.SetFont("s9 w1000", "Arial")  ; font size 9, bold
MyNotification.Add("Text", "cBlack Center", mytext)  ; black text
MyNotification.Show("x1650 y985 NoActivate")  ; NoActivate avoids deactivating the currently active window
WinMove xAxis, yAxis,,, MyNotification
if timer = 1
    SetTimer EndMyNotif, myduration * -1
if timer = 0 {
    Sleep myduration
    EndMyNotif
    }
EndMyNotif() {
    MyNotification.Destroy()
    }
}

Error Box -

Spoiler

Any help solving this would be appreciated.
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Unable to Destroy/Hide a Gui, Gui.Destroy() causes Error  Topic is solved

23 Dec 2023, 00:37

2 things:
- your gui and your function should have different names.
- make your gui name global
Simplified:

Code: Select all

MyGuiFn("Hello World", 5000, 1)

F12::
	{
	MyGui.Destroy()
	}

MyGuiFn(mytext, myduration, timer)
	{
	global MyGui
	MyGui := Gui()
	MyGui.Add("Text", "cBlack Center", mytext)
	MyGui.Show("NoActivate")
	if timer = 1
		SetTimer EndMyNotif, - myduration
	EndMyNotif()
		{
		MyGui.Destroy()
		}
	}
14.3 & 1.3.7

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: AccelAHK, Google [Bot], Soren, vmech and 48 guests