Jump to content

Exit Func Loop with button press...


guysmilie
 Share

Recommended Posts

I have a Func that starts a loop when a button is pressed. How do allow the user to exit this loop by pressing another button, or the same button? Say Func in the script looks like this:

Func BUTTON1()

Do

Do some stuff when BUTTON1 is pressed, and repeat

***What can I add here to exit the loop if a button is pressed?

Until 1 = 2

EndFunc

I am using "OnEvent" mode.

I'm totally new to this so any and all help is greatly appreciated. Thanks in advance to anyone who can assist me.

Guy.

Edited by guysmilie
Link to comment
Share on other sites

Here is an example using MessageLoop mode. I'm sure you could adapt it to OnEvent mode.

#include <GUIConstantsEx.au3>

$Form1_1 = GUICreate("My Form", 343, 195, 192, 124)
$Label1 = GUICtrlCreateLabel("Ready to Loop", 64, 16, 198, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Goudy Old Style")
GUICtrlSetColor(-1, 0x808000)
$Button1 = GUICtrlCreateButton( "Button1", 216, 104, 75, 25)
$Button2 = GUICtrlCreateButton( "Button2", 216, 144, 75, 25)

GUICtrlSetState($button2, $GUI_DISABLE)

GUISwitch($Form1_1)
GUISetState(@SW_SHOW)
$button1Loop = False

While 1
$nMsg = GUIGetMsg(1)

Select
 Case $nMsg[0] = $Button2
  MsgBox(0,"Button2","Button 2 clicked")
  $button1Loop = False
  GUICtrlSetState($button1, $GUI_ENABLE)
  GUICtrlSetState($button2, $GUI_DISABLE)
  GUICtrlSetData($label1, "All Done")
  case $nMsg[0] = $Button1
   MsgBox(0, "title", "Button1 Clicked")
   $button1Loop = True
  GUICtrlSetState($button2, $GUI_ENABLE)
  GUICtrlSetState($button1, $GUI_DISABLE)
   Button1()
  case $nMsg[0] = $GUI_EVENT_CLOSE and $nMsg[1] = $Form1_1
   ExitLoop
EndSelect

if $button1Loop Then
 Button1()
EndIf

WEnd

Func Button1()
GUICtrlSetData($label1, "Looping: " &TimerInit())
EndFunc
Link to comment
Share on other sites

Thanks for your reply,

Perhaps using MessageLoop mode would have been a better way for me to have written the script.

However now that I have already done everything in OnEvent mode, is there not a way to have a button press exit the loop running withing a Func ?

Thanks for the assistance,

Guy.

Edited by guysmilie
Link to comment
Share on other sites

I am no GUI expert, but the following would work. May not be a best practice. By the way, the GUISwitch function in the prior example was a leftover from something else and is extraneous.

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)

$Form1_1 = GUICreate("My Form", 343, 195, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClk")
$Label1 = GUICtrlCreateLabel("Ready to Loop", 64, 16, 198, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Goudy Old Style")
GUICtrlSetColor(-1, 0x808000)
$Button1 = GUICtrlCreateButton( "Button1", 216, 104, 75, 25)
$Button2 = GUICtrlCreateButton( "Button2", 216, 144, 75, 25)
GUICtrlSetOnEvent($Button1,"Button1")
GUICtrlSetOnEvent($button2,"Button2")

GUICtrlSetState($button2, $GUI_DISABLE)

GUISetState(@SW_SHOW)
$button1Loop = False

While 1
 sleep(100)
 if $button1Loop Then
  Button1Loop()
 EndIf
WEnd

Func Button1()
 GUICtrlSetState($button2, $GUI_ENABLE)
 GUICtrlSetState($button1, $GUI_DISABLE)
 $button1Loop = True
EndFunc

Func Button1Loop()
GUICtrlSetData($label1, "Looping: " &TimerInit())

EndFunc

Func Button2()
  $button1Loop = False
  GUICtrlSetState($button1, $GUI_ENABLE)
  GUICtrlSetState($button2, $GUI_DISABLE)
  GUICtrlSetData($label1, "All Done")
EndFunc

Func CLOSEClk()
 Exit
EndFunc
Link to comment
Share on other sites

Here is an example using MessageLoop mode. I'm sure you could adapt it to OnEvent mode.

#include <GUIConstantsEx.au3>

$Form1_1 = GUICreate("My Form", 343, 195, 192, 124)
$Label1 = GUICtrlCreateLabel("Ready to Loop", 64, 16, 198, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Goudy Old Style")
GUICtrlSetColor(-1, 0x808000)
$Button1 = GUICtrlCreateButton( "Button1", 216, 104, 75, 25)
$Button2 = GUICtrlCreateButton( "Button2", 216, 144, 75, 25)

GUICtrlSetState($button2, $GUI_DISABLE)

GUISwitch($Form1_1)
GUISetState(@SW_SHOW)
$button1Loop = False

While 1
$nMsg = GUIGetMsg(1)

Select
 Case $nMsg[0] = $Button2
  MsgBox(0,"Button2","Button 2 clicked")
  $button1Loop = False
  GUICtrlSetState($button1, $GUI_ENABLE)
  GUICtrlSetState($button2, $GUI_DISABLE)
  GUICtrlSetData($label1, "All Done")
  case $nMsg[0] = $Button1
   MsgBox(0, "title", "Button1 Clicked")
   $button1Loop = True
  GUICtrlSetState($button2, $GUI_ENABLE)
  GUICtrlSetState($button1, $GUI_DISABLE)
   Button1()
  case $nMsg[0] = $GUI_EVENT_CLOSE and $nMsg[1] = $Form1_1
   ExitLoop
EndSelect

if $button1Loop Then
 Button1()
EndIf

WEnd

Func Button1()
GUICtrlSetData($label1, "Looping: " &TimerInit())
EndFunc

Link to comment
Share on other sites

OK, that works. I will try to add this code to my script and hopefully it will work just as well as it does in your example.

TYVM for your assistance. I really appreciate you spending time helping me out. Hopefully someday when I get better at this I can do the same for someone.

Guy.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...