Sign in to follow this  
Followers 0
BlueC

Simple Toggle Problem

5 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I thought having $Set as a global variable would reassign it when it was passed as a parameter.

This fix worked like a charm, much appreciated!

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

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
Sign in to follow this  
Followers 0