Jump to content

Simple Toggle Problem


BlueC
 Share

Recommended Posts

I'm creating a program that uses a constantly floating tooltip to display data. I wish to allow the option to display short data (1 line) or longer data (3 lines) in that tooltip, determined by a toggle switch assigned to a keypress (" ] ").

Code is roughly as follows.

Global $Set ; Toggle setting
Global $Tip ; Text string inserted into Tooltip function
Global $LineA, $LineB, $LineC ; Text strings inserted into $Tip variable

...
If _IsPressed('DD') Then
 If $Set = 0 Then
  Toggle(1)
  sleep(500)
 Else
  Toggle(0)
  sleep(500)
 EndIf
EndIf
...

Func Toggle($Set)
 $Tip = 0
 $Tip = $LineA
 If $Set = 1 Then
  $Tip = $Tip & @CRLF & $LineB & @CRLF & $LineC
 EndIf
EndFunc

If I initialize the toggle $Set to 0 (short data), then I am able to easily switch to 1 (long data).

However if either $Set is initialized to 1 or toggled from 0 to 1, then it is unable to toggle back to 0. I've tried switching the If-Else statement but I run into the same problem. Is there something preventing the toggle from working in reverse?

Thanks in advance for any input.

Edited by BlueC
Link to comment
Share on other sites

You have to set $Set. Example:

#include <Misc.au3>

Global $Set = 0 ; Toggle setting
Global $Tip ; Text string inserted into Tooltip function
Global $LineA, $LineB, $LineC ; Text strings inserted into $Tip variable

While 1
    If _IsPressed("1B") Then Exit ; ESC to exit
    If _IsPressed("71") Then      ; F2 to toggle
        If $Set = 0 Then
            $Set = 1
            Toggle($Set)
        Else
            $Set = 0
            Toggle($Set)
        EndIf
        Sleep(250)
    EndIf
WEnd

Func Toggle($Set)
    $Tip = 0
    $Tip = $LineA
    If $Set = 1 Then
        ConsoleWrite("Long" & @CRLF)
        $Tip = $Tip & @CRLF & $LineB & @CRLF & $LineC
    Else
        ConsoleWrite("Short" & @CRLF)
    EndIf
EndFunc   ;==>Toggle
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

A parameter as you use it has a local scope. After you exit the function the parameter is gone.

If you need to change a global variable you pass as parameter you can use keyword "ByRef" in the function header:

Func Toggle(ByRef $Set)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

....

...
Func Toggle($Set)
$Tip = 0
$Tip = $LineA
....
....

In your example, "$Tip = 0" is not needed because of the following line also assigns the same variable, $Tip.

Here is another example.

Note: The nested While-Wend loop allows for the reduction of the Sleep() value in the main. This makes the main While-Wend loop more responsive to key presses.

#include <Misc.au3>

;Global $Set = 0 ; Declare globally or as Static within the Toggle function.
Global $Tip ; Text string inserted into Tooltip function
Global $LineA, $LineB, $LineC ; Text strings inserted into $Tip variable

While 1
    If _IsPressed("1B") Then Exit ; ESC to exit
    If _IsPressed("71") Then
        While _IsPressed("71") ; While F2 key is held down. Release F2 key to exit loop - guarantee one F2 key press only.
            Sleep(10)
        WEnd
        Toggle() ; F2 to toggle
    EndIf
    Sleep(10)
WEnd

Func Toggle()
    Static $Set = 0 ; Delete this line if $Set is declared globally. (CAUTION - See Static in help file)
    $Set = Not $Set ; Toggles $Set value
    $Tip = $LineA
    If $Set = 1 Then
        ConsoleWrite("Long" & @CRLF)
        $Tip &= @CRLF & $LineB & @CRLF & $LineC
    Else
        ConsoleWrite("Short" & @CRLF)
    EndIf
EndFunc   ;==>Toggle
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...