Please help with gui button toggle

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Please help with gui button toggle

18 Oct 2013, 19:59

I know there's a way to do this... and I know I'm attempting to do it the wrong way.

Can someone lend a hand please?

I need the button to toggle between Start/Stop, and at the same time I want the while loop to break when the "Stop" button is pressed.

Code: Select all

#SingleInstance Force

Gui, Add, Button, w90 h30 Default gButton vButton, Start
Gui, Show
return

GuiClose:
ExitApp

Button:
	Toggle := !Toggle
	While Toggle
	{
		Sleep 50
		GuiControl,, Button1, Stop 
		Sleep 50
		ToolTip Main Loop is Running
	}
	ToolTip
	GuiControl,, Button1, Start 
Return
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Please help with gui button toggle

18 Oct 2013, 20:16

Using a timer instead of a loop is probably the answer, or at least one possible answer:

Code: Select all

#SingleInstance Force

Gui, Add, Button, w90 h30 Default gButton vButton, Start
Gui, Show
return

GuiClose:
ExitApp

Button:
    Toggle := !Toggle
	if (Toggle)
		GuiControl,, Button1, Stop
	
Label:
	Sleep 50
	ToolTip Main Loop is Running
    if (Toggle)
		SetTimer, Label, -50
	else
        GuiControl,, Button1, Start
Return
Last edited by kon on 18 Oct 2013, 20:18, edited 1 time in total.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Please help with gui button toggle

18 Oct 2013, 20:17

I'm not seeing the While loop in your example kon?

That loop needs to stay intact... I use it for the main actions of the script.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Please help with gui button toggle

18 Oct 2013, 20:22

Code: Select all

#SingleInstance Force

Gui, Add, Button, w90 h30 Default gButton vButton, Start
Gui, Show
return

GuiClose:
ExitApp

Button:
    Toggle := !Toggle
    if (Toggle)
        GuiControl,, Button1, Stop
   
Label:
    Sleep 50
    ToolTip Main Loop is Running
    if (Toggle)
    {
        ;Put the contents of the while loop here
        SetTimer, Label, -50
    }
    else
        GuiControl,, Button1, Start
Return
Will this allow you to carry out the main actions of your script?
Edit: added more context to the code
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Please help with gui button toggle

18 Oct 2013, 20:26

Thanks for the shove in the right direction... here's what I came up with. (not sure how it compares with your latest example)

Code: Select all

#SingleInstance Force

Gui, Add, Button, w90 h30 Default gButton vButton, Start
Gui, Show
return

GuiClose:
ExitApp

Button:
    Toggle := !Toggle
    if (Toggle)
        GuiControl,, Button1, Stop
    
Main:
    Sleep 50
    ToolTip Main Loop is Running
    If(Toggle)
        SetTimer, Main, -50
    else
	{
        GuiControl,, Button1, Start
		ToolTip
	}
Return
just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Please help with gui button toggle

19 Oct 2013, 01:18

Code: Select all

#NoEnv
#SingleInstance Force

Gui, Add, Button, w90 h30 Default gButton vButton, Start
Gui, Show
Return

GuiClose:
ExitApp

Button:
   GuiControlGet, Action, , %A_GuiControl%
   GuiControl, , %A_GuiControl%, % (Action = "Start" ? "Stop" : "Start")
   If (Action = "Start")
      SetTimer, MainLoop, -1
Return

MainLoop:
   Iterations := 1
   While (Action = "Start") {
      Sleep, 250
      ToolTip, % "MainLoop iteration # " . Iterations++
   }
   ToolTip
Return
?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Please help with gui button toggle

19 Oct 2013, 04:59

I always use two buttons: Start and Stop.

Code: Select all

Gui, Margin, 50, 50
Gui, Add, Button, w100 h40 vbtn_start gbtn_start, Start
Gui, Add, Button, ys wp hp vbtn_stop gbtn_stop Disabled, Stop
Gui, Show
Return

btn_stop:
	GuiControl, Disable, btn_stop
	IsStoped := True
Return

StopRun:
	GuiControl, Enable, btn_start
	ToolTip
Return

btn_start:
	IsStoped := False
	GuiControl, Disable, btn_start
	GuiControl, Enable, btn_stop
	Goto, LoopRun
Return

LoopRun:
	if IsStoped
		Goto, StopRun

	Count += 1
	ToolTip, % Count
	Sleep, 1000

	; More code ...

	Goto, LoopRun
Return

GuiClose:
ExitApp
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Please help with gui button toggle

22 Oct 2013, 08:08

Love the use of ternary, just me.

I've just started to use that in my scripts and hadn't thought about using it with this too!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 134 guests