MyGui.Destroy() does not work ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 02:57

Hi @all
I'm testing AHKv2
here is a short script to test the GUI-function
my problem is, that »MyGui.Destroy()« does not Remove the window
it remains on the screen.
After reloading the script the window will be removed (off course)
What's wrong ?

Code: Select all

^1::						;Strg+1 
	{
		I:="0"
		again:
		I++
		MyGui:=Gui()
		MyGui.Opt("+AlwaysOnTop -Caption -SysMenu +Owner") 
		MyGui.BackColor := "blue"
		MyGui.SetFont("cWhite s20 q5" , "Segoe")
		MyGui.Add("Text","x20 y20" ,"I= " I)
		MyGui.Show("x1100 y600 autosize")
		
		if I>5
		 MyGui.Destroy()	;<----- will not remove the window
		
		If msgbox("I = " I,,"R/C T10") = "Cancel"
		{
		 MyGui.Destroy() 
		 msgbox("exit",,"T1")
		 exit
		}
		goto again
	} 
my OS is Windows 10
need help
regards
MS

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?

Post by teadrinker » 09 Feb 2023, 03:40

Change MyGui.Show() like this:

Code: Select all

MyGui.Show("autosize y100 x" . 500 + I*200)

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 04:02

@teadrinker
thx but the window still will not be removed

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?

Post by teadrinker » 09 Feb 2023, 04:08

How many windows did you see after 4'th iteration? How many after 5'th? 6, 7, 8?

neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

Re: MyGui.Destroy() does not work ?

Post by neogna2 » 09 Feb 2023, 04:16

Adding to what teadrinker is saying: each goto loop creates a new Gui but keeps the previous ones, only they're on top of each other so you don't see them. Starting at iteration 6 the script creates a new Gui and then immediately destroys it. Leaving Gui 5 visible.
Like teadrinker said change to MyGui.Show("autosize y100 x" . 500 + I*200) and you'll see what is going on.
Last edited by neogna2 on 09 Feb 2023, 04:18, edited 1 time in total.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?

Post by teadrinker » 09 Feb 2023, 04:17

To understand what's going on you can add an extra MsgBox:

Code: Select all

^1::						;Strg+1 
	{
		I:="0"
		again:
		I++
		MyGui:=Gui()
		MyGui.Opt("+AlwaysOnTop -Caption -SysMenu +Owner") 
		MyGui.BackColor := "blue"
		MyGui.SetFont("cWhite s20 q5" , "Segoe")
		MyGui.Add("Text","x20 y20" ,"I= " I)
		MyGui.Show("autosize y100 x" . 0 + I*200)

		if I>5 {
            MsgBox 'Going to destroy GUI'
            MyGui.Destroy()	;<----- will not remove the window
        }
		
		If msgbox("I = " I,,"R/C T10") = "Cancel"
		{
		 MyGui.Destroy() 
		 msgbox("exit",,"T1")
		 exit
		}
		goto again
	}

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 04:37

thx
if I change the "Gui.show" line to

Code: Select all

MyGui.Show("autosize y100 x" . 0 + I*200)
i see 5 separate windows in a line side by side
these windows are not removed either after execution of

Code: Select all

MyGui.Destroy()	

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?

Post by teadrinker » 09 Feb 2023, 04:40

Hm, did you launch my previous code with an extra MsgBox as it is? What did you see?

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 05:01

@teadrinker
yes i add the msgbox
what i now see is, that after excecution of

Code: Select all

MyGui.Destroy()
the last added window (#6) will be removed
the windows #1 -#5 are still on the screen
btw.
this script should be act later as a "status line" window
in which the line "I=" is constantly changing after each loop
and what i need is a fixed postion on the screen

neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

Re: MyGui.Destroy() does not work ?

Post by neogna2 » 09 Feb 2023, 05:07

Maxsteinfeld wrote:
09 Feb 2023, 04:37
i see 5 separate windows in a line side by side
these windows are not removed either after execution of

Code: Select all

MyGui.Destroy()	
They're not removed because Gui objects 1-5 are no longer accessible through variable MyGui because on each loop you reassign that variable to a new Gui. On loop 6 it is Gui 6 and therefore MyGui.Destroy() only destroys Gui 6. And on next loop Gui 7, then 8, ...
Maxsteinfeld wrote:
09 Feb 2023, 04:37
this script should be act later as a "status line" window
Sounds like you could then create a single Gui and only use a loop to update its text.

Code: Select all

MyGui := Gui()
MyText := MyGui.Add("Text", , "hello")
MyGui.Show("w200 h100")
Sleep(1000)
Loop 5
{
  MyText.Value := A_Index
  Sleep(1000)
}
ExitApp

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?

Post by teadrinker » 09 Feb 2023, 05:13

Maxsteinfeld wrote: the last added window (#6) will be removed
the windows #1 -#5 are still on the screen
Yep, the Destroy() method can destroy just a window it belongs to, nothing else.
Maxsteinfeld wrote: this script should be act later as a "status line" window
in which the line "I=" is constantly changing after each loop
But why you are creating a new GUI every time? Create it just once and then change its text:

Code: Select all

^1::						;Strg+1 
	{
		I:="0", MyGui := ""
		again:
		I++
        if !MyGui {
            MyGui:=Gui()
            MyGui.Opt("+AlwaysOnTop -Caption -SysMenu +Owner") 
            MyGui.BackColor := "blue"
            MyGui.SetFont("cWhite s20 q5" , "Segoe")
            MyText := MyGui.Add("Text","x20 y20" ,"I= 1")
            MyGui.Show("x1100 y600 autosize")
        }
        else {
            MyText.Text := "I= " I
        }
		if I>5 {
            MyGui.Destroy()	;<----- will not remove the window
            return
        }
		
		If msgbox("I = " I,,"R/C T10") = "Cancel"
		{
		 MyGui.Destroy() 
		 msgbox("exit",,"T1")
		 exit
		}
		goto again
	}

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 05:39

@teadrinker
@neogna2
thx a lot for the help
now i understand ....
both selutions works pretty

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 07:30

@teadrinker
I have one more question:
what would the code be like under AHKv1 ?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyGui.Destroy() does not work ?  Topic is solved

Post by teadrinker » 09 Feb 2023, 11:09

Maxsteinfeld wrote: what would the code be like under AHKv1 ?

Code: Select all

#Requires AutoHotkey v1

^1::
    if !hGui {
        Gui, New, +hwndhGui +AlwaysOnTop -Caption +ToolWindow
        Gui, Color, Blue
        Gui, Font, cWhite s20, Segoe
        Gui, Add, Text, vMyText x20 y20, I= 0
    }
    Loop 5 {
        Gui, %hGui%: Default
        GuiControl,, MyText, % t := "I= " . A_Index
        Gui, Show, NA x1100 y600
        MsgBox, 5,, % t, 10
        IfMsgBox, Cancel
        {
            MsgBox,,, exit, 1
            break
        }
    }
    Gui, Hide
Return
More optimal v2 code:

Code: Select all

#Requires AutoHotkey v2

^1:: {
    if !IsSet(MyGui) {
        MyGui := Gui('+AlwaysOnTop -Caption +ToolWindow')
        MyGui.BackColor := 'Blue'
        MyGui.SetFont('cWhite s20', 'Segoe')
        MyText := MyGui.Add('Text', 'x20 y20', 'I= 1')
    }
    Loop 5 {
        MyText.Text := 'I= ' . A_Index
        MyGui.Show('NA x1100 y600')
        if MsgBox(MyText.Text,, 'R/C T10') = 'Cancel' {
            MsgBox 'exit',, 'T1'
            break
        }
    }
    MyGui.Hide()
}
As you can see, I don't destroy the GUI, I just show and hide it.

Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: MyGui.Destroy() does not work ?

Post by Maxsteinfeld » 09 Feb 2023, 16:09

wow !
thx a lot
will test the sctipts in peace
regards
MS

Post Reply

Return to “Ask for Help (v2)”