Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Changing button text


  • Please log in to reply
15 replies to this topic
Guest 100
  • Guests
  • Last active:
  • Joined: --
What would be the easiest way to do this ..

I have a GUI with a button "Start". When I hit the start button, I do a ControlSetText command to change the button to "Stop".

Now, when I click that "Stop" button, how do I make it jump to the ButtonStop label? (and change the button text back to "Start")

My GUI is really small and have no room for seperate Start and Stop buttons.

Any help would be greatly appreciated!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Here is a template. Please refer AHK doc for explanations. :)

Gui +ToolWindow
Gui, Add, Button, w100 h25 g[color=red]StartStop[/color] v[color=red]ButtonText[/color], Start
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" ) 
   {
     GuiControl,, ButtonText, Stop
     GoSub, Start
   }
Else
   { 
     GuiControl,, ButtonText, Start
     GoSub, Stop
   }
Return

Start:
Return

Stop:
Return

GuiClose:
 ExitApp


Guest100
  • Guests
  • Last active:
  • Joined: --
Thank you!

Guest100
  • Guests
  • Last active:
  • Joined: --
Is this possible?

Have it goto the start label instead of "gosub". I want to run a bunch of commands there.

Thing is, when I hit the "stop" button, I want to stop whatever it's doing immediately (via the reload command).

Guest100
  • Guests
  • Last active:
  • Joined: --
Here is an example :

WinSet, AlwaysOnTop, On, TestGUI
GUI, Submit, NoHide

GuiControlGet, ButtonText
If ( ButtonText = "START" )
   {
     GuiControl,, ButtonText, STOP
     Goto, Start
   }
Else
   {
     GuiControl,, ButtonText, START
     Goto, Stop
   }
Return





Start:
Loop,
{
	envadd, count, 1
	sleep, 100
}



Stop:
msgbox, %count%


Is there any way to program in the stop button to do the "reload" command WITHOUT having to put code in my start loop?

The reason for that is I'm doing a lot of commands very fast and sometimes things are getting backed up .. there will be a delay if I add code in the loop to stop everything. SO If I do hit "stop" it may take 5-10 secs before anything is realized. I need this to be immedate.

Thanks!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

when I hit the "stop" button, I want to stop whatever it's doing immediately (via the reload command).


Gui +ToolWindow
Gui, Add, Button, w100 h25 gStartStop vButtonText, Start
Gui, Add, StatusBar
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" ) 
   {
     GuiControl,, ButtonText, Stop
     SetTimer, Start, -1
   }
Else
   Reload
   
Return

Start:
  Loop {
         Count ++ 
         SB_SetText( "`t" Count )
         Sleep 250
       }
Return

GuiClose:
 ExitApp

:?: :!:

Guest100
  • Guests
  • Last active:
  • Joined: --
Perfect! I modified it a litte bit though.

One last question .. is the "Reload" command the only way to reset everything back to normal?

Is there a way to stop everything (and change the button text back to "start") just like we first loaded the GUI?

So I can "start" and "stop" many times without having to reload the script?

Here is what I have :

Gui, Add, Button, w100 h25 gStartStop vButtonText, Start
Gui, Add, StatusBar
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" )
   {
     GuiControl,, ButtonText, Stop
     SetTimer, Start, -1
     return
   }
Else
   MsgBox, %count%
   Reload
   
Return



Start:
Loop
{
EnvAdd, count,1
}



GuiClose:
 ExitApp


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
untested

Gui, Add, Text, w100 h25 vcountText, Press Start
Gui, Add, Button, w100 h25 gStartStop vButtonText, Start
Gui, Add, StatusBar
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" )
   {
     GuiControl,, ButtonText, Stop
     SetTimer, counter, 1000   ;changed from -1 to make it count ~seconds
   }
Else
  {
    GuiControl,, ButtonText, Start
    SetTimer, counter, off  
    MsgBox, %count%
    count = 0
    GuiControl,, countText, Press Start
  }   
Return



counter:
count++
GuiControl,, countText, %count%
return

GuiClose:
 ExitApp

I have made a few different unrelated changes. you can put the counter loop back the way it was if you want, but there is no telling how much processor it was taking up.

Guest100
  • Guests
  • Last active:
  • Joined: --
Works great!

The only reason I did that Loop count was to use as much processor as possible. I wanted to test the "stop" button to make sure it was immediate under those conditions (which it does).

Thanks!

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
why are you trying to use up processor?

Guest100
  • Guests
  • Last active:
  • Joined: --
To make sure the "stop" button would still work under heavy conditions.

Guest100
  • Guests
  • Last active:
  • Joined: --
Ran in to a weird problem ..

The code below runs fine the first time, but if I hit the stop button and then the start button again .. It completely skips the "Start" label and automatically goes to "Start1".


Gui, Add, Text, w100 h25 vcountText, Press Start 
Gui, Add, Button, w100 h25 gStartStop vButtonText, Start 
Gui, Add, StatusBar 
Gui, Show, , Test 
Return 

StartStop: 
GuiControlGet, ButtonText 
If ( ButtonText = "Start" ) 
   { 
     GuiControl,, ButtonText, Stop 
     SetTimer, Start, 1000
   } 
Else 
  { 
    GuiControl,, ButtonText, Start 
    SetTimer, Start, off  
    MsgBox, %count% 
    count = 0 
    GuiControl,, countText, Press Start 
  }    
Return 


Start:
Msgbox, We are starting
goto, Start1


Start1:
Loop
{
EnvAdd, count, 1
}


GuiClose: 
 ExitApp 


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
you can remove the extra text I added, since you are not using it.

Gui, Add, Text, w100 h25 vcountText, Press Start
Gui, Add, Button, w100 h25 gStartStop vButtonText, Start
Gui, Add, StatusBar
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" )
   {
     GuiControl,, ButtonText, Stop
     SetTimer, Start, -1
   }
Else
  {
    GuiControl,, ButtonText, Start
    MsgBox, %count%
    count = 0
  }   
Return


Start:
Msgbox, We are starting
goto, Start1
return   ;not needed, but I think it looks better

Start1:
Loop
{
;EnvAdd, count, 1
count++
}


GuiClose:
 ExitApp 

*untested

you also kept my settimer at 100, while looping the count, so it would start a new thread looping constantly...

Guest100
  • Guests
  • Last active:
  • Joined: --
Thanks for the speedy reply but yeah, I have already tried that and it still skips the "Start" routine when you start and stop multiple times.

If I use the "reload" command it works fine, but I was hoping that wasn't necessary.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
It's not that it skips right to the Start1 part, but that it never even Starts the Start routine the second time.

The Start1 loop is still running from the first time you started it, so the second time is on hold.

tested:
Gui, Add, Text, w100 h25 vcountText, Press Start
Gui, Add, Button, w100 h25 gStartStop vButtonText, Start
Gui, Add, StatusBar
Gui, Show, , Test
Return

StartStop:
GuiControlGet, ButtonText
If ( ButtonText = "Start" )
   {
     GuiControl,, ButtonText, Stop
     Break = 0
     SetTimer, Start, -1
   }
Else
  {
    GuiControl,, ButtonText, Start
    MsgBox, %count%
    Break = 1
    count = 0
  }   
Return


Start:
Msgbox, We are starting
goto, Start1
return   ;not needed, but I think it looks better

Start1:
Loop
{
;EnvAdd, count, 1
count++
If (Break = 1)
   Break
}
Return

GuiClose:
 ExitApp