Sign in to follow this  
Followers 0
friends

How to toggle a butotn on / off ?

7 posts in this topic

I'm stuck on how to toggle a button on (show) or off (hide) by

pressing a same key ? Eg. F5

Anybody ?

Share this post


Link to post
Share on other sites



Piece of cake. Here it is:

#include <GUIConstants.au3>
AutoItSetOption("TrayIconDebug", 1)

;Initialize variables
Global $style1
Global $IniFile
Global $GUIWidth
Global $GUIHeight

$GUIWidth = 250
$GUIHeight = 250
$IniFile = StringTrimRight(@ScriptFullPath, 3) & "ini"

;Only a close button:
;$style1 = BitOR($WS_POPUP, $WS_CAPTION, $WS_SYSMENU)
;GUICreate("New GUI", $GUIWidth, $GUIHeight, -1, -1, $style1)

HotKeySet("{F5}", "SwapButton")

GUICreate("Swap Button", $GUIWidth, $GUIHeight)
   $button_1 = GUICtrlCreateButton("OK", 50, 150, 70, 20)

GUISetState(@SW_SHOW)

While 1
   Sleep(25)
   $msg = GUIGetMsg()
   Select
   
      Case $msg = $GUI_EVENT_CLOSE
         GUIDelete()
         Exit

   EndSelect

WEnd

Func SwapButton()
   If NOT IsDeclared("ButtonState") Then Global $ButtonState = "On"
   If $ButtonState = "On" Then
      $ButtonState = "Off"
      GUICtrlSetState($button_1, $GUI_DISABLE)
   ElseIf $ButtonState = "Off" Then
      $ButtonState = "On"
      GUICtrlSetState($button_1, $GUI_ENABLE)
   EndIf
EndFunc

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

SlimShady, thanks for prompt feedback.

I know you're always being a helpful guy !

Edited by friends

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Just as an alternative: You could test the current state to swap like:

Func SwapButton()
  If GUICtrlGetState($button_1) = $GUI_ENABLE Then 
     GUICtrlSetState($button_1, $GUI_DISABLE)
  Else
     GUICtrlSetState($button_1, $GUI_ENABLE)
  EndIf
EndFunc
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Just as an alternative: You could test the current state to swap like:

Func SwapButton()
  If GUICtrlGetState($button_1) = $GUI_ENABLE Then 
     GUICtrlSetState($button_1, $GUI_DISABLE)
  Else
     GUICtrlSetState($button_1, $GUI_ENABLE)
  EndIf
EndFunc

<{POST_SNAPBACK}>

Thanks JdeB.... I will try it out. :)

Share this post


Link to post
Share on other sites

Great idea JdeB.

I tried it out and it didn't work so I tried to find out why.

Here's a modified version that works:

Func SwapButton()
   If GUICtrlGetState($button_1) = $GUI_ENABLE + $GUI_SHOW Then
      GUICtrlSetState($button_1, $GUI_DISABLE)
   Else
      GUICtrlSetState($button_1, $GUI_ENABLE)
   EndIf
EndFunc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Great idea JdeB.

I tried it out and it didn't work so I tried to find out why.

Here's a modified version that works:

Func SwapButton()
   If GUICtrlGetState($button_1) = $GUI_ENABLE + $GUI_SHOW Then
      GUICtrlSetState($button_1, $GUI_DISABLE)
   Else
      GUICtrlSetState($button_1, $GUI_ENABLE)
   EndIf
EndFunc

<{POST_SNAPBACK}>

Yea... i didn't test it and didn't think of the other states it could have.

This version should only test for ENABLED state only:

Func SwapButton()
  If BitANd(GUICtrlGetState($button_1),$GUI_ENABLE) Then
     GUICtrlSetState($button_1, $GUI_DISABLE)
  Else
     GUICtrlSetState($button_1, $GUI_ENABLE)
  EndIf
EndFunc
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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