Jump to content
31290

Show button when clicking a checkbox

Recommended Posts

31290

Hi everyone, 

I'm trying to display a button once a checkbox is clicked in my main GUI and to hide said button when the said checkbox is unticked:

#include <ButtonConstants.au3>
#Include <Constants.au3>
#include <Date.au3>
#include <File.au3>
#Include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>

Opt("GUIOnEventMode", 1)

f_MainGui()

While 1
   Sleep (10)
Wend

Func f_MainGui()
   Global $g_MainGUI = GUICreate ("SEE Delete Temp Files", 400, 450)
   GUISetFont (8.5, 700, 0)
   GUISetBkColor ($Color_White)
   $g_Label1 = GUICtrlCreateLabel ("Select action(s):", 150, 96, 360, 17)
   $g_Label2 = GUICtrlCreateLabel ("Web Browsers:", 13, 125, 360, 17)
   Global $g_ChkBox1 = GUICtrlCreateCheckbox ("I.Explorer [ Cookies / Temp Internet Files / Tracking Data]", 16, 152, 360, 17)
   Global $g_ChkBox2 = GUICtrlCreateCheckbox ("Chrome", 16, 176, 120, 17)
   Global $g_ChkBox3 = GUICtrlCreateCheckbox ("Firefox", 16, 200, 120, 17)
   $g_Label3 = GUICtrlCreateLabel ("Windows Explorer:", 13, 231, 360, 17)
   Global $g_ChkBox4 = GUICtrlCreateCheckbox ("C:\Temp", 16, 255, 120, 17)
   Global $g_ChkBox5 = GUICtrlCreateCheckbox ("C:\Windows\Temp", 16, 279, 130, 17)
   Global $g_ChkBox6 = GUICtrlCreateCheckbox ("C:\Users\XXXXXX\Recent", 16, 303, 200, 17)
   Global $g_ChkBox7 = GUICtrlCreateCheckbox ("C:\Users\XXXXXX\Local Settings\Temp", 16, 327, 250, 17)
   Global $g_SelWB = GUICtrlCreateButton ("Select All Web Browsers", 16,360,150,25)
   GUICtrlSetOnEvent (-1, "f_SelectAllWB")
   Global $g_SelWinExpl = GUICtrlCreateButton (" Windows Explorer Only", 16,390,150,25)
   GUICtrlSetOnEvent (-1, "f_WinExplOnly")
   Global $g_SelAll = GUICtrlCreateButton ("All", 16, 420, 70, 25)
   GUICtrlSetOnEvent (-1, "f_SelAll")
   Global $g_SelAll = GUICtrlCreateButton ("None", 95, 420, 70, 25)
   GUICtrlSetOnEvent (-1, "f_SelNone")
   Global $g_SubmitAndClean = GUICtrlCreateButton ("Clean Temp Files", 264, 360, 120, 85)
   GUICtrlSetState ($g_SubmitAndClean, $GUI_HIDE)
   GUICtrlSetOnEvent (-1, "f_SumitAndClean")
   GUISetOnEvent ($GUI_EVENT_CLOSE, "_exit")
   GUISetState()
EndFunc

I tried everything ==> "Do...Until", "While...Wend", hotkeys etc... and the best I can have a a button that can't stop flicking. 

I work exclusively with Events and for sure, when going trough the non event method,  Switch...Case...EndSwitch, it's working...

I'm sure that this may be a beginner question but I think my mind stopped working after an all day coding on different projects :)

Thanks in advance :)


~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
AutoBert

and which of the checkBoxes is the one which should toggle buttonstate? And where are the func's for the events?

Here's a small script: ArrayOfCheckBoxesOE.au3, which shows the logic you need if any checkbox should enable Button.

Edited by AutoBert
Code polished
  • Like 2

Share this post


Link to post
Share on other sites
InunoTaishou

GUICtrlSetPos

Also, it's bad practice to create global variables inside a function. Declare them outside f_MainGui and initialize them in your function.

#include <ButtonConstants.au3>
#Include <Constants.au3>
#include <Date.au3>
#include <File.au3>
#Include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>

Global $hGui
Global $btnExample
Global $chkExample

Opt("GUIOnEventMode", 1)
Example()

While 1
   Sleep (100)
Wend

Func Example()
    $hGUI = GUIcreate("Example", 120, 80)
    $btnExample = GUICtrlCreateButton("Hidden Button", 400, 0, 100, 30)
    $chkExample = GUICtrlCreateCheckbox("Click me!", 10, 10, 100, 30)
    GUICtrlSetOnEvent($btnExample, "ButtonExample")
    GUICtrlSetOnEvent($chkExample, "SetButton")
    GUISetOnEvent($GUI_EVENT_CLOSE, "Close")

    GUISetState(@SW_SHOW, $hGUI)
EndFunc

Func SetButton()
    If (GUICtrlRead($chkExample) = $GUI_CHECKED) Then
        GUICtrlSetPos($btnExample, 10, 40, 100, 30)
    Else
        GUICtrlSetPos($btnExample, 400, 0, 100, 30)
    EndIf
EndFunc

Func ButtonExample()
    MsgBox("", "", "Hidden button has been clicked!")
EndFunc

Func Close()
    GUIDelete($hGUI)
    Exit 0
EndFunc

 

  • Like 1

Share this post


Link to post
Share on other sites
31290
12 hours ago, AutoBert said:

and which of the checkBoxes is the one which should toggle buttonstate? And where are the func's for the events?

Here's a small script: ArrayOfCheckBoxesOE.au3, which shows the logic you need if any checkbox should enable Button.

Thanks for the small script, Indeed I can see clearly now. To answer your question, the button needs to be enabled while at least a checkbox is checked. But, In my case, I think I can't create them with arrays. I'll manage to find a way to mix your example with my work but not sure if this will be possible. Maybe you know the answer :D
The functions event are in a separate file, under construction (yeah, I like to have separate files and include them afterward ^^)

12 hours ago, InunoTaishou said:

GUICtrlSetPos

Also, it's bad practice to create global variables inside a function. Declare them outside f_MainGui and initialize them in your function.

You are totally right, I moved them in the beginning of the main script file:

;Options
Opt("GUIOnEventMode", 1)

Global $g_ChkBox1, $g_ChkBox2, $g_ChkBox3, $g_ChkBox4, $g_ChkBox5, $g_ChkBox6, $g_ChkBox7, $g_SelWinExpl, $g_SelAll, $g_SelNone, $g_SubmitAndClean

But, your example, even if it helped me a lot understand how things work here, put the button away if a checkbox is unticked whereas another one is ticked. Which is logical due to the "if" statement.

EDIT: OK, I found the way to do what I needed:

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc

func activeBtn()
  If _IsChecked($g_ChkBox1) or _IsChecked($g_ChkBox2) or _IsChecked($g_ChkBox3) or _IsChecked($g_ChkBox4) or _IsChecked($g_ChkBox5) or _IsChecked($g_ChkBox6) or _IsChecked($g_ChkBox7)  Then
    GUICtrlSetPos($g_SubmitAndClean,  264, 360, 120, 85)
  Elseif not _IsChecked($g_ChkBox1) or _IsChecked($g_ChkBox2) or _IsChecked($g_ChkBox3) or _IsChecked($g_ChkBox4) or _IsChecked($g_ChkBox5) or _IsChecked($g_ChkBox6) or _IsChecked($g_ChkBox7) Then
    GUICtrlSetPos($g_SubmitAndClean,  1000, 360, 120, 85)
  Endif
EndFunc

And after each checkbox creation:

$g_ChkBox1 = GUICtrlCreateCheckbox ("I.Explorer [ Cookies / Temp Internet Files / Tracking Data]", 16, 152, 360, 17)
   GUICtrlSetOnEvent (-1, "activebtn")
   $g_ChkBox2 = GUICtrlCreateCheckbox ("Chrome", 16, 176, 120, 17)
   GUICtrlSetOnEvent (-1, "activebtn")
   $g_ChkBox3 = GUICtrlCreateCheckbox ("Firefox", 16, 200, 120, 17)
   GUICtrlSetOnEvent (-1, "activebtn")

And so on...

Thanks both of you for pointing me help and some good practices I'm gonna adopt from now :)

Edited by 31290

~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
AutoBert

To store the ControlID's in array, is the shortest and easiest way to solve your problem. I have changed the script. (The original was with coloured buttons, Search&Replace wasn't the best way to adapt the logic to checkboxes).

The logic i used need's a Array with 2 Col's (1. col to store CtrlID, 2. col to store functionname to be called when submit button is pressed and checkbox is checked) and for each checkbox 1 Row. 

If you can't use 1 loop for creating the checkboxes (because Taborder and Buttons), you will have a bit more lines in your script for creating, but the logic is also working when all checkboxes are in one array.  When there's no need to start any other action, as checking if at least 1 CheckBox is checked, the CtrlID's mustn't be in continous range (3,4,5 ...). In my first example it was only needed for toggling the colour. So i can't see anything against storing the CtrlID's in array.     

Edited by AutoBert

Share this post


Link to post
Share on other sites
spudw2k
Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc

func activeBtn()
  If _IsChecked(@GUI_CtrlId) Then
    GUICtrlSetPos($g_SubmitAndClean,  264, 360, 120, 85)
  Else
    GUICtrlSetPos($g_SubmitAndClean,  1000, 360, 120, 85)
  Endif
EndFunc

Simpler...yes?

Share this post


Link to post
Share on other sites
AutoBert
13 hours ago, spudw2k said:
Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc

func activeBtn()
  If _IsChecked(@GUI_CtrlId) Then
    GUICtrlSetPos($g_SubmitAndClean,  264, 360, 120, 85)
  Else
    GUICtrlSetPos($g_SubmitAndClean,  1000, 360, 120, 85)
  Endif
EndFunc

Simpler...yes?

No, you only check one checkbox, so it doesn't work. This solution with stored CtrlId's in array is the simplest way:

Func _Click()
    ;Local $iBtn = @GUI_CtrlId - $aBtnIds[0][0]
    ;ConsoleWrite($iBtn+1&' = '&'CtrlID: '&@GUI_CtrlId&@CRLF)
    Local $bAtLeastOneCheckBoxChecked
    For $i = 0 To 8
        If BitAND(GUICtrlRead($aBtnIds[$i][0]),$GUI_checked)=$GUI_checked Then
            $bAtLeastOneCheckBoxChecked = True
            ExitLoop
        EndIf
    Next
    If $bAtLeastOneCheckBoxChecked Then
        GUICtrlSetState($idBtnSubmit, $GUI_ENABLE)
    Else
        GUICtrlSetState($idBtnSubmit, $GUI_DISABLE)
    EndIf
EndFunc   ;==>_Click

 

Share this post


Link to post
Share on other sites
spudw2k

Perhaps if he performed a different action based on each individual checkbox control, but in this case it doesn't matter which control is checked...just check the one control that fired the function to see if it is checked.  Much more efficient...or am I missing something?  :blink:

 

edit: missing ingredient found.  The OP wanted more-or-less an OR condition handled by X  checkboxes (If any checkbox is checked then 1, else 0).  To catch the 0 state, all checkboxes need to be evaluated.  I get it.  


Now if one would want to take a different action for each checkbox, then yes...I agree that utilizing an array is better than declaring each control.

 

edit: I see in your previous response (using an array) that you mention having different functions for each control, which is smart...but in response to the OP's solution checking the "firing" checkbox is all that was needed to meet their criteria.

Edited by spudw2k

Share this post


Link to post
Share on other sites
AutoBert
4 hours ago, spudw2k said:

Perhaps ...

Nothing perhaps:

On 20.2.2016 at 1:44 PM, 31290 said:

To answer your question, the button needs to be enabled while at least a checkbox is checked.

Please read thread before answering.

Edited by AutoBert

Share this post


Link to post
Share on other sites
spudw2k
Cirusnb
On 2/19/2016 at 8:46 PM, AutoBert said:

and which of the checkBoxes is the one which should toggle buttonstate? And where are the func's for the events?

Here's a small script: ArrayOfCheckBoxesOE.au3, which shows the logic you need if any checkbox should enable Button.

thank you, 

I might be able to use this a current issue i'm having with a project i'm working on.  

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

  • Similar Content

    • Pike
      By Pike
      Hello Everyone,
      New Guy here. This is a two-part question, hopefully easy enough though. I know it's ugly coding, this is because I have only a couple hours worth of coding experience and could really use some help.
      1) When I single click the button I want one action (like playing a sound clip) and when I double click the button I want a different action (such as opening dialogue box to ask for soundclip directory address). At the bottom of the script I made a comment which one I would like single click versus double click.
      2) I can not get the FileOpenDialogue to save the file address to the config.ini file at the 'One' position on the .ini.
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <AutoItConstants.au3> #include <Sound.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $OneID _Main() Func _Main() GUICreate("Form1", 156, 86, 281, 276) $OneID = GUICtrlCreateButton("Button", 15, 17, 122, 37) GUICtrlSetOnEvent($OneID, "OnOne") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While 1 While 1 Sleep(1000) WEnd WEnd EndFunc ;---------------Functions------------- While 1 Sleep(100) WEnd Func OnOne() ; ******************Single Click for this action**************** $OneID = IniRead("config.ini", "Config", "One", $OneID) If $OneID = True Then SoundPlay($OneID, @HotKeyPressed) Else ; *******Double Click for this action*****And Help Saving File Address to config.ini 'One'*********** $OneID = FileOpenDialog("Select file", @WorkingDir, "All (*.*)") IniWrite("config.ini", "Config", "One", $OneID) $OneID = InputBox("Enter Information", "Short Name for File?", "") IniWrite("config.ini", "Config", "NameOne", $OneID) EndIf EndFunc Any help would be greatly appreciated, and if you need anymore clarity as to what exactly I'm trying to do, please do not hesitate to ask. Thanks so much!
      Pike
    • kcvinu
      By kcvinu
      Hi all,
      I have a button created with _GUICtrlButton_Create function. I know that this function returns a handle of button instead of an ID. So i have obtain control ID by calling _WinAPI_GetDlgCtrlID function. After that i have tried the GUICtrlSetBkColor function. But it didnt worked. Any suggestions will be appreciated. Thanks in advance. 
      Note : - The window which carries this button is not created with native gui create function. Instead, it is created with CreateWindowEx api function.
    • rudi
      By rudi
      Hello,
       
      for some script I need to investigate the states checked/unchecked, hidden/shown, enabled/disabled to several checkbox controls.
       
      Any suggestions to this script to retrieve the information in a better way?
      ; Autoit Version 3.3.14.2 ; GuiCtrlCheckState.au3 #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $CtrlW = 100 $CtrlH = 30 $GuiW = 250 $GuiH = 300 GUICreate("Test Checkbox Status", $GuiW, $GuiH) $Ctrl = GUICtrlCreateCheckbox("Checkbox", 10, 10, $CtrlW, $CtrlH) Opt("Guicoordmode", 2) $BtnDisable = GUICtrlCreateButton("Disable", -1, 10) $BtnEnable = GUICtrlCreateButton("Enable", -1, 1) $BtnHide = GUICtrlCreateButton("Hide", 10, -$CtrlH * 2 - 1) $BtnUnHide = GUICtrlCreateButton("Show (Unhide)", -1, 1) $BtnCheckChecked = GUICtrlCreateButton("IsChecked", -$CtrlW * 2 - 10, 40) $BtnCheckEnabled = GUICtrlCreateButton("IsEnabled", -1, 1) $BtnCheckHidden = GUICtrlCreateButton("IsHidden", -1, 1) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $BtnDisable GUICtrlSetState($Ctrl, $gui_disable) Case $BtnEnable GUICtrlSetState($Ctrl, $GUI_Enable) Case $BtnHide GUICtrlSetState($Ctrl, $GUI_Hide) Case $BtnUnHide GUICtrlSetState($Ctrl, $GUI_show) Case $BtnCheckChecked If CheckState($Ctrl, $GUI_CHECKED) Then ConsoleWrite("Checked" & @CRLF) Else ConsoleWrite("not checked" & @CRLF) EndIf Case $BtnCheckEnabled If CheckState($Ctrl, $GUI_Enable) Then ConsoleWrite("enabled" & @CRLF) Else ConsoleWrite("disabled" & @CRLF) EndIf Case $BtnCheckHidden If CheckState($Ctrl, $GUI_Hide) Then ConsoleWrite("Hidden" & @CRLF) Else ConsoleWrite("not hidden" & @CRLF) EndIf EndSwitch WEnd Func CheckState($_CtrlID, $_State = $GUI_CHECKED) ConsoleWrite("---------------------" & @CRLF) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_State to check = ' & $_State & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_Read = GUICtrlRead($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_Read (GuiCtrlRead) = ' & $_Read & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_StateFound = GUICtrlGetState($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_StateFound (GuiCtrlGetState) = ' & $_StateFound & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $_StateFound = -1 Then MsgBox(48, "Control-ID not defined", "Control-ID invalid") Return False ElseIf BitAND($_StateFound, $_State) = $_State Then Return True ElseIf BitAND($_Read, $_State) = $_State Then Return True Else Return False EndIf EndFunc ;==>CheckState Regards, Rudi.
    • Robdog1955
      By Robdog1955
      I'm trying to click a button on a web page. I have added a couple of MsgBox lines to allow me to watch what happens on the page. As you can see the first half of my script enters data into text boxes on the page. I have no problem there. I just cannot click on the region buttons. The "set focus" line causes an outline to appear around the EU button and the "click button" line causes the "Pick a Region" text to disappear. Here is the code I have so far.
      #include <IE.au3> Local $oIE = _IECreate("http://questchecker.com/") Local $iQuestID = "123456" Local $sCharacterName = "CharacterName" Local $colForms = _IEFormGetCollection($oIE) $iCount = 0 For $oForm In $colForms $oFormElements = _IEFormElementGetCollection($oForm) For $oFormElement In $oFormElements $iCount = $iCount + 1 Local $sTagName = StringLower($oFormElement.tagName) Local $sElementType = $oFormElement.type Local $sElementName = $oFormElement.name Switch $iCount Case 6 _IEFormElementSetValue($oFormElement, "MyRealm", 0) ; realm Case 7 _IEFormElementSetValue($oFormElement, $sCharacterName, 0) Case 8 _IEFormElementSetValue($oFormElement, $iQuestID, 0) EndSwitch Next Next Local $oButtons = _IEGetObjByName($oIE, "questForm") For $oButton In $oButtons If _IEFormElementGetValue($oButton) = "US" Then MsgBox(0, "", "Click Okay to set focus") _IEAction($oButton, "focus") MsgBox(0, "", "Click Okay to click button") _IEAction($oButton, "click") ExitLoop EndIf Next MsgBox(0, "", "Click Okay to quit") _IEQuit($oIE) Exit  
    • gillesg
      By gillesg
      Hello,
      I am struggling in merging GUITreeViewEx, Shelltristate and enhancing to handle a third state that means : some items under are selected.
      I have difficulties handling expand order and key Space (especially when node is collapsed).
      Here the zip with UDF and and example.
       
      The problem I might need some advice to handle : 
      1- When load Treeview, have a correct settings of the checkbox for a tristate tree
      2 - Handle keyboard used for walking in tree
           Chicken is checked and  Steak is unchecked
          When walking with arrow to Meat, it gets unchecked
      3 - When node is collapsed and checked thru keyboard (space)
         the middle state is possible which should not
      Here is joined an animated gif showing the 3 problems
       
      Thanks for your advices
       
       
       
       
       
       
       
       
       
       
       

      GUITreeview3Ex.zip
×