Jump to content

Button update (toggle function) problem


Audrey
 Share

Recommended Posts

Hi Guys,

I'm facing a problem when updating a button (color and text) after clicking.

When clicking the button again, it has to update the prevous color and text, but it doesn't work.

Is there someone who can help me out with this ??

Thanks in advance !!!

Audrey

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Global $Main, $ConnectBtn, $msg, $State

$Main = GUICreate("ButtonTest", 600, 600)
   $ConnectBtn = GUICtrlCreateButton("Connect", 50, 50, 130, 20)
GUISetState()

$State = "0"

While 1
   $msg = GUIGetMsg()
   If $msg = $ConnectBtn AND $State = "0" Then Call ("Connect")
   If $msg = $ConnectBtn AND $State = "1" Then Call ("Disconnect")
   If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd

Func Connect ()
   GUICtrlSetData($ConnectBtn, "Disconnect")
   GUICtrlSetBkColor($ConnectBtn, 0xFF0000)  ; red
   $State = "1"
EndFunc

Func Disconnect ()
   GUICtrlSetData($ConnectBtn, "Connect")
   GUICtrlSetBkColor($ConnectBtn, 0x00FF00)  ; green
EndFunc
Link to comment
Share on other sites

I've cleaned up the code a little bit, and corrected the mistake in the original.

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Global $Main, $ConnectBtn, $msg, $State

$Main = GUICreate("ButtonTest", 600, 600)
$ConnectBtn = GUICtrlCreateButton("Connect", 50, 50, 130, 20)
GUISetState()

$State = "0"

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $ConnectBtn
            If $State = 0 Then
                Connect()
            Else
                Disconnect()
            EndIf
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func Connect()
    GUICtrlSetData($ConnectBtn, "Disconnect")
    GUICtrlSetBkColor($ConnectBtn, 0xFF0000) ; red
    $State = 1
EndFunc

Func Disconnect()
    GUICtrlSetData($ConnectBtn, "Connect")
    GUICtrlSetBkColor($ConnectBtn, 0x00FF00) ; green
    $State = 0 ; <<<<<<<<<<<<<<<<<<<<< you never reset the value of $State
EndFunc

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi Brew,

Thank you for your answer !!

Apart from my "$State=0" mistake, do you know why my code with the IF-AND-THEN routine is not working?

My simple mind can't get a grip on that one...

Gr,

Audrey

Edited by Audrey
Link to comment
Share on other sites

  • Developers

The first if is true which runs disconnect and then the second is is also true running Connect.

This is a minimal change to your script to demo that including some debug lines showing what is happening when ran from SciTE.

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Global $Main, $ConnectBtn, $msg, $State
$Main = GUICreate("ButtonTest", 600, 600)
$ConnectBtn = GUICtrlCreateButton("Connect", 50, 50, 130, 20)
GUISetState()$State = "0"
While 1
$msg = GUIGetMsg()
If $msg = $ConnectBtn And $State = "0" Then
  Connect()
elseIf $msg = $ConnectBtn And $State = "1" Then
  Disconnect()
EndIf
If $msg = $GUI_EVENT_CLOSE Then Exit
WEndFunc Connect()
GUICtrlSetData($ConnectBtn, "Disconnect")
GUICtrlSetBkColor($ConnectBtn, 0xFF0000) ; red
$State = "1"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $State = ' & $State & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
EndFunc   ;==>Connect
Func Disconnect()
GUICtrlSetData($ConnectBtn, "Connect")
GUICtrlSetBkColor($ConnectBtn, 0x00FF00) ; green
$State = "0"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $State = ' & $State & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
EndFunc   ;==>Disconnect

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The multiple If/Then statements are being run in sequence, so what was happening is that it read the first one that called function Connect, did what it had to do in that function, then returned to the While loop. Because your If statements are run 1 after the other, you can't release the button fast enough for it to not go into the Disconnect function immediately and it resets the $State variable back to 0. So every time you hit the button, it activates Connect then immediately activates Disconnect.

The way I wrote it, the If/Then will only be read once, so $State stays at 1 or 0 because the variable $msg gets reset the next time through the loop.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi Jos and Brew,

Thanks (again) for pointing this out !!

I did not realize that my laptop was faster than me! :)

Also thanks for the console function. That could help me out a lot in the near future as (one of few ???) female AutoIt scripters... ;)

Miss A

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