Jump to content

[How To] Toggle Action?

Recommended Posts


My brain hurts.. I cannot seem to figure out the best way to toggle an action.. For instance..

Do action, and wait for an event , toggle the action, wait for another event. But... Don't want the event to trigger the toggle but one time..

So, say in my while statement.. I want to check to see if a location has an existing color? and if that color changes. Do something ONCE.. and wait for the color to leave. Then Do the opposite action. ONCE.  In other words if i'm looking for green and its always gray, I don't want the gray state constantly firing off the action? and likewise a green state won't keep firing the action for green.

Is this the best way? (I know you guys like to see code attempts before offering suggestions.. lol)

Local $toggle = False
While 1
    If FileExists(@ScriptDir & "\test.txt") And Not ($Toggle = True) Then
        MsgBox(0, "title", "Toggle on!")
        $Toggle = True
    ElseIf Not FileExists(@ScriptDir & "\test.txt") And ($Toggle = True) Then
        MsgBox(0, "title", "Toggle off!")
        $Toggle = False


Edited by zone97



WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]


Share this post

Link to post
Share on other sites

You want to change Else to ElseIf otherwise it will continue to fire for example:

#include <GUIConstantsEx.au3>

Func _Example()
Local $bToggle = True
GUICreate('Toggle', 200, 40)
Local $hColor = GUICtrlCreateLabel('Green', 5, 10, 100, 20)
Local $hButton = GUICtrlCreateButton('Toggle', 110, 10, 85, 20)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
            Case $hButton
                GUICtrlSetData($hColor, 'Grey')
        If GUICtrlRead($hColor) = 'Green' And $bToggle = True Then
            MsgBox(48,'', GUICtrlRead($hColor))
            $bToggle = False
        ElseIf GUICtrlRead($hColor) <> 'Green' And $bToggle = False Then
            MsgBox(48,'', GUICtrlRead($hColor))
            $bToggle = True


Share this post

Link to post
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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.