Jump to content

Show button when clicking a checkbox


31290
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 3 weeks later...
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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 months later...
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.  

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

×
×
  • Create New...