Jump to content

How to start stop Exiting the array For loop with the button?


 Share

Recommended Posts

Hi friends, how do I use the start and stop button when pulling sequences from the for loop?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
Local $aArray[10] = ["Num1", "Num2", "Num3", "Num4", "Num5", "Num6", "Num7", "Num8", "Num9", "Num10"]

$Form1 = GUICreate("Form1", 489, 353, 192, 130)
$ButtonStartStop = GUICtrlCreateButton("Start", 176, 248, 75, 25)
$Edit1 = GUICtrlCreateEdit("", 104, 48, 233, 129)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonStartStop
            For $i = 0 To UBound($aArray)-1
                If GUICtrlRead($ButtonStartStop) = "Stop" Then
                    GUICtrlSetData($ButtonStartStop, "Start")
                    Sleep(100)
                ExitLoop
                Else
                     GUICtrlSetData($ButtonStartStop, "Stop")
                GUICtrlSetData($Edit1, $aArray[$i] & @CRLF,1)
                EndIf
                Next
    EndSwitch
WEnd

 

Edited by youtuber
Link to comment
Share on other sites

whats the endgame?  Most things that "loop through a set of selections until you press stop" are not actually looping through the things. They are just waiting for you to press stop and pseudo-randomly selecting one, especially if there is no need to present the user a visual to even pretend that's what is going on.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

of course, but a big ass Press Your Luck board seems overkill.

i question the need for a loop at all. Why not just present a directory when they press stop, having done the math you want for it's selection?

number of items per second in the loop * how many seconds they took to press it, modulo ubound-1.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Pull messages in the loop: $msg = GUIGetMsg()

It's not beautiful, but it's what you're asking for I think.

$counterStart = 0
For $i = $counterStart To UBound($aArray)-1
  If GUIGetMsg() = $idButton Then
      $counterStart = $i
      ExitLoop
  EndIf
Next

Never-mind, I'm not prepared to solve this atm.  I shouldn't have said anything.  My bad :)

Edited by Xandy
Link to comment
Share on other sites

This is what you are describing, i believe.  But i still question the need to actually iterate through the loop whilst waiting for the stop button to be pressed....

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$fInterrupt = 0
$hGUI = GUICreate("Test", 100, 100)
$hButton_1 = GUICtrlCreateButton("START", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("STOP", 10, 50, 80, 30)
$Counter = 1
GUISetState()
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            $Counter = _Func_1() + 1
        Case $hButton_2
            _Func_2()
    EndSwitch
WEnd

Func _Func_1()
    $fInterrupt = 0
    For $i = $Counter To 30
        ConsoleWrite("SelectItem: " & $i & @CRLF)
        If $fInterrupt <> 0 Then
            ConsoleWrite("!STOPPED AT ITEM " & $i & @CRLF)
            Return $i
        EndIf
        Sleep(100)
        If $i = 30 Then $i = 0
    Next
EndFunc

Func _Func_2()
    return
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

 

modified from Melbas here:

And if I know how many seconds elapsed between when you started and when you pressed the button, then I know where you stopped and where I need to resume.  The actual visualization is nice but not ancillary.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

youtuber,

Another of many ways to do this...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <file.au3>

Local $aFiles = _FileListToArrayRec('c:\k\autoit', '*', 1, 1, 1, 2)
If @error Then Exit MsgBox(17, 'Error', @extended)

$Form1 = GUICreate("My File List")
$ButtonStartStop = GUICtrlCreateButton("Start", 10, 375, 380, 20)
$Edit1 = GUICtrlCreateEdit("", 10, 10, 380, 360)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonStartStop
            If GUICtrlRead($ButtonStartStop) = "Stop" Then
                GUICtrlSetData($ButtonStartStop, "Start")
                AdlibUnRegister('Files')
                ContinueLoop
            EndIf
            If GUICtrlRead($ButtonStartStop) = 'Start' Then
                GUICtrlSetData($ButtonStartStop, "Stop")
                AdlibRegister('Files', 100)
            EndIf
    EndSwitch
WEnd

Func Files()

    Local Static $idx = 0
    GUICtrlSetData($Edit1, $aFiles[$idx] & @CRLF, 1)
    $idx += 1

EndFunc   ;==>Files

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

...and the last one, based on the 3rd example from here, the OnEventMode way  :)

#include <GUIConstantsEx.au3>

Opt("GuiOnEventMode", 1)
Global $stop = 1

Local $aArray[10] = ["Num1", "Num2", "Num3", "Num4", "Num5", "Num6", "Num7", "Num8", "Num9", "Num10"]

$Form1 = GUICreate("Form1", 489, 353, 192, 130)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$ButtonStartStop = GUICtrlCreateButton("Start", 176, 248, 75, 25)
GUICtrlSetOnEvent(-1, "_stop")
$Edit1 = GUICtrlCreateEdit("", 104, 48, 233, 129)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
    If not $stop Then _loop()
WEnd

  
Func _stop()
    $stop = not $stop
EndFunc

Func _loop()
    Local Static $index
    For $i = $index To UBound($aArray)-1
        If $stop Then 
            GUICtrlSetData($ButtonStartStop, "Start")
            Exitloop
        Else
            $index += 1
            GUICtrlSetData($ButtonStartStop, "Stop")
            GUICtrlSetData($Edit1, $aArray[$i] & @CRLF,1)
            If $index = UBound($aArray) Then GUICtrlSetData($ButtonStartStop, "End !")
            Sleep(500)
        EndIf
    Next
EndFunc

Func _exit()
    Exit Msgbox(0,"", "bye")
EndFunc

 

Edit
"I do not understand what you said."
Yes. @iamtheky sometimes has an alien way of thinking that we humans cant understand ... :unsure:

 

 

Edited by mikell
Link to comment
Share on other sites

@mikell I can not use a second case :(

#include <GUIConstantsEx.au3>

Opt("GuiOnEventMode", 1)
Global $stop = 1

Local $aArray[10] = ["Num1", "Num2", "Num3", "Num4", "Num5", "Num6", "Num7", "Num8", "Num9", "Num10"]

$Form1 = GUICreate("Form1", 489, 353, 192, 130)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$ButtonStartStop = GUICtrlCreateButton("Start", 176, 248, 75, 25)
GUICtrlSetOnEvent(-1, "_stop")
$Button2 = GUICtrlCreateButton("Button2", 320, 248, 75, 25)
$Edit1 = GUICtrlCreateEdit("", 104, 48, 233, 129)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
    If not $stop Then _loop()

    $nMsg = GUIGetMsg()
    Switch $nMsg

Case $Button2
        MsgBox(0,"","Button case")

    EndSwitch
WEnd

Func _stop()
    $stop = not $stop
EndFunc

Func _loop()
    Local Static $index
    For $i = $index To UBound($aArray)-1
        If $stop Then
            GUICtrlSetData($ButtonStartStop, "Start")
            Exitloop
        Else
            $index += 1
            GUICtrlSetData($ButtonStartStop, "Stop")
            GUICtrlSetData($Edit1, $aArray[$i] & @CRLF,1)
            If $index = UBound($aArray) Then GUICtrlSetData($ButtonStartStop, "End !")
            Sleep(500)
        EndIf
    Next
EndFunc

Func _exit()
    Exit Msgbox(0,"", "bye")
EndFunc

 

Link to comment
Share on other sites

5 hours ago, mikell said:

"I do not understand what you said."
Yes. @iamtheky sometimes has an alien way of thinking

I do my best to speak how i code, by nesting all my thoughts together in one line and hitting F5.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@mikell How can I run a regex code here?

#include <GUIConstantsEx.au3>
#include <Array.au3>
Opt("GuiOnEventMode", 1)
Global $stop = 1

Local $aArray[10] = ["Num1", "Num2", "Num3", "Num4", "Num5", "Num6", "Num7", "Num8", "Num9", "Num10"]

$Form1 = GUICreate("Form1", 489, 353, 192, 130)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$ButtonStartStop = GUICtrlCreateButton("Start", 176, 248, 75, 25)
GUICtrlSetOnEvent(-1, "_stop")
$Edit1 = GUICtrlCreateEdit("", 104, 48, 233, 129)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
    If not $stop Then _loop()
WEnd


Func _stop()
    $stop = not $stop
EndFunc

Func _loop()
    Local Static $index
    For $i = $index To UBound($aArray)-1
        If $stop Then
            GUICtrlSetData($ButtonStartStop, "Start")
            Exitloop
        Else
            $index += 1
            $arrayregex = StringRegExp($aArray[$i], 'Num(.?)', 3)
            ;~ _ArrayDisplay($arrayregex)
            GUICtrlSetData($ButtonStartStop, "Stop")
            GUICtrlSetData($Edit1, $arrayregex & @CRLF,1)
            If $index = UBound($aArray) Then GUICtrlSetData($ButtonStartStop, "End !")
            Sleep(500)
        EndIf
    Next
EndFunc

Func _exit()
    Exit
EndFunc

 

Link to comment
Share on other sites

It depends on what the regex is intended to do. Using flag 3 it returns an array, but an Edit displays strings, not arrays
In the particular case of your current code the array has one element only so something like this may work

$arrayregex = StringRegExp($aArray[$i], 'Num(.?)', 3)
            ;~ _ArrayDisplay($arrayregex)
            ; GUICtrlSetData($ButtonStartStop, "Stop")
            If IsArray($arrayregex) Then GUICtrlSetData($Edit1, $arrayregex[0] & @CRLF,1)

but in the real life you will probably need _ArrayToString or something else

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...