Jump to content
31290

Show button when clicking a checkbox

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

Share this post


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

 

Share this post


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

Share this post


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

Share this post


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

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

    • By ozymandius257
      I'm trying to check the status of the checkbox for sharing a folder that you see when you right-click on a  folder, go to the sharing tab, then click on Advanced Sharing.

      The check box is labelled Share this folder, and looking at it using AutoIt window info, I get the following information..
      >>>> Control <<<<
      Class:    Button
      Instance:    1
      ClassnameNN:    Button1
      Name:    
      Advanced (Class):    [CLASS:Button; INSTANCE:1]
      ID:    1020
      Text:    &Share this folder
      Position:    15, 16
      Size:    306, 13
      ControlClick Coords:    57, 4
      Style:    0x50010003
      ExStyle:    0x00000004
      Handle:    0x0000000000420A52
      I'm using the following code..
      If ControlCommand("Sharing","","[CLASS:Button; INSTANCE:1]","IsChecked","") = 0 Then
          MsgBox(0, "", "not checked")
      Else
          MsgBox(0, "", "checked")
      EndIf
      But this insists the check box isn't checked when it is.  I have also tried ControlCommand("Sharing","","[ID:1020]","IsChecked","") and ControlCommand("Sharing","","Share this folder","IsChecked","") and ControlCommand("Sharing","","&Share this folder","IsChecked",""), with no joy.
       
      Any ideas?
    • By GillesMaisonneuve
      Good morning,
       
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?
      TIA
      Gilles

    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
       
      Much Respect,
      Pike
       
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

    • By misioooo
      Hi!
       
      I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
      Part with button looks like this:
      $ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1, $hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
    • By XinYoung
      Quick question: How can I click this "Select All" button in IE? There's no ID, and the href is "#"  
      Am I looking at the wrong html code?
      Am I looking
      Thank you all in advance!
×
×
  • Create New...