31290

Disabling a checked checkbox

12 posts in this topic

#1 ·  Posted (edited)

Hi everyone, 

Question is simple:

I have 12 checkboxes and I'm looking for a way to grey out a checkbox as soon as it is ticked by the End User.

Func ManualActions()
Local $ManualActionsGui = GuiCreate("Automated Task List Suite", 460, 480, -1, -1)
    GUISetBkColor ($Color_White)
    GUICtrlCreatePic ($Images & "\SAClogo.jpg", 110, 10, 240, 80)
    GUICtrlCreateLabel ("---MANUAL ACTIONS---" , 173, 100, 300, -1)
    GUICtrlSetFont (-1, 8.5, 700, 0)
    GUICtrlCreateCheckbox("Put a label with hostname on the computer?", 5,  120, -1,-1)
        GUICtrlSetOnEvent(-1, "OKTicked")
    GUICtrlCreateCheckbox("Add all applications in AD?", 5,  140, -1,-1)
    GUICtrlCreateCheckbox("Check the installed drivers?", 5,  160, -1,-1)
    GUICtrlCreateCheckbox("Customize all desktop icons?", 5,  180, -1,-1)
    GUICtrlCreateCheckbox("Restore User's data?", 5,  200, -1,-1)
    GUICtrlCreateCheckbox("Connect to the VPN once?", 5,  220, -1,-1)
    GUICtrlCreateCheckbox("Update Firefox and Google Chrome ?", 5,  240, -1,-1)
    GUICtrlCreateCheckbox("Restore user's Bookmarks?", 5,  260, -1,-1)
    GUICtrlCreateCheckbox("Deploy CrashPlanPro, remove new licence and send a mail to the Service Desk?", 5,  280, -1,-1)
    GUICtrlCreateCheckbox("Check if printers the User needs are present?", 5,  300, -1,-1)
    GUICtrlCreateCheckbox("Copy SAPlogin.ini from former Computer (if needed)?", 5,  320, -1,-1)
    GUICtrlCreateCheckbox("Encrypt the Disk with PGP?", 5,  340, -1,-1)
    GUICtrlCreateCheckbox("Update Remedy database?", 5,  360, -1,-1)
    While 1
        GUISetState ()
        Local $ManualActionsMsg = GUIGetMsg ()
        Switch $ManualActionsMsg 
        Case $GUI_EVENT_CLOSE
                Exit
            EndSwitch
            WEnd
EndFunc
Func OKTicked()
GUICtrlSetState ("-1",  $GUI_DISABLE)
EndFunc

 

I've read about "GuiCtrlSetState" and GUICtrlSetOnEvent but I can't figure that.

How Can I do that please?

Thanks :)

Edited by 31290

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You'll have to store the ControlID in a variable and use that variable in GUICtrlSetState() like so:

Local $myVar = GUICtrlCreateCheckbox("Example", 5, 360)
GUICtrlSetState($myVar, $GUI_DISABLE)
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks MikahS, 

So it means that I have to create 12 more variables and also 12 GuiCtrlSetState?  :tv_horror:

Hum, a loop or a for statement cannot do the trick? (I'm also not good for these statements)

Thanks :)

Edited by 31290

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Yes, a for loop would be recommended like so:

#include <GUIConstants.au3>

Local $gui, $array[5]

$gui = GUICreate("TEST", 200, 200)
$array[0] = GUICtrlCreateCheckbox("test", 5, 10)
$array[1] = GUICtrlCreateCheckbox("test", 5, 30)
$array[2] = GUICtrlCreateCheckbox("test", 5, 50)
$array[3] = GUICtrlCreateCheckbox("test", 5, 70)
$array[4] = GUICtrlCreateCheckbox("test", 5, 90)
GUISetState()

While 1
    $msg = GUIGetMsg()
   Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(100)
    checkTheBoxes()
WEnd

Func checkTheBoxes()
    Local $i
    For $i = 0 To Ubound($array) - 1 Step 1
        If GUICtrlRead($array[$i]) = 1 Then
            GUICtrlSetState($array[$i], 128)
        EndIf
    Next
EndFunc
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here are another couple of ideas to chew on.  Depending on what your requirements really are (which I'll bet you'll want to firm up) the following will do what you asked for. 

Here I created a simple function ("OKTicked") that each checkbox will fire when clicked.  It simply checks the state of the checkbox that fired the func and if it is checked it disables the checkbox control.  Rather than adding a GUICtrlSetOnEvent after the creating of each checkbox I opted to use another func for the Checkbox Ctrl creation.)

Opt("GUIOnEventMode", 1)
Global Const $GUI_DISABLE = 128
Global Const $GUI_EVENT_CLOSE = -3
ManualActions()
Func ManualActions()
    Local $ManualActionsGui = GuiCreate("Automated Task List Suite", 460, 480, -1, -1)
    GUISetBkColor (0xFFFFFF)
    ;GUICtrlCreatePic ($Images & "\SAClogo.jpg", 110, 10, 240, 80)
    GUICtrlCreateLabel ("---MANUAL ACTIONS---" , 173, 100, 300, -1)
    GUICtrlSetFont (-1, 8.5, 700, 0)
 _CreateCheckbox("Put a label with hostname on the computer?", 5,  120)
    _CreateCheckbox("Add all applications in AD?", 5,  140)
    _CreateCheckbox("Check the installed drivers?", 5,  160)
    _CreateCheckbox("Customize all desktop icons?", 5,  180)
    _CreateCheckbox("Restore User's data?", 5,  200)
    _CreateCheckbox("Connect to the VPN once?", 5,  220)
    _CreateCheckbox("Update Firefox and Google Chrome ?", 5,  240)
    _CreateCheckbox("Restore user's Bookmarks?", 5,  260)
    _CreateCheckbox("Deploy CrashPlanPro, remove new licence and send a mail to the Service Desk?", 5,  280)
    _CreateCheckbox("Check if printers the User needs are present?", 5,  300)
    _CreateCheckbox("Copy SAPlogin.ini from former Computer (if needed)?", 5,  320)
    _CreateCheckbox("Encrypt the Disk with PGP?", 5,  340)
    _CreateCheckbox("Update Remedy database?", 5,  360)
 GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $ManualActionsGUI)
 GUISetState ()
 While 1
        sleep(10)
    WEnd
EndFunc
Func OKTicked()
   If GUICtrlRead(@GUI_CtrlId) = 1 Then GUICtrlSetState(@GUI_CtrlId, $GUI_DISABLE)
EndFunc
Func _CreateCheckbox($sLabel, $iX, $iY)
   GUICtrlCreateCheckbox($sLabel, $iX,  $iY, -1,-1)
   GUICtrlSetOnEvent(-1, "OKTicked")
EndFunc
Func _Exit()
   Exit
EndFunc

And in this example I used an Array to hold the "labels' for each checkbox and then create them in a loop.

Opt("GUIOnEventMode", 1)
Global Const $GUI_DISABLE = 128
Global Const $GUI_EVENT_CLOSE = -3
ManualActions()
Func ManualActions()
    Local $ManualActionsGui = GuiCreate("Automated Task List Suite", 460, 480, -1, -1)
    GUISetBkColor (0xFFFFFF)
    ;GUICtrlCreatePic ($Images & "\SAClogo.jpg", 110, 10, 240, 80)
    GUICtrlCreateLabel ("---MANUAL ACTIONS---" , 173, 100, 300, -1)
    GUICtrlSetFont (-1, 8.5, 700, 0)
 Local $aCheckboxes[13] = ["Put a label with hostname on the computer?", "Add all applications in AD?", "Check the installed drivers?", "Customize all desktop icons?", _
   "Restore User's data?", "Connect to the VPN once?", "Update Firefox and Google Chrome ?", "Restore user's Bookmarks?", "Deploy CrashPlanPro, remove new licence and send a mail to the Service Desk?", _
   "Check if printers the User needs are present?", "Copy SAPlogin.ini from former Computer (if needed)?", "Encrypt the Disk with PGP?", "Update Remedy database?"]
 Local $iX = 5, $iY = 120
 For $x = 0 to 12
    ConsoleWrite($iY & @CRLF)
     _CreateCheckbox($aCheckboxes[$x], $iX, $iY)
    $iY += 20
    Next
 GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $ManualActionsGUI)
 GUISetState ()
 While 1
        sleep(10)
    WEnd
EndFunc
Func OKTicked()
   If GUICtrlRead(@GUI_CtrlId) = 1 Then GUICtrlSetState(@GUI_CtrlId, $GUI_DISABLE)
EndFunc
Func _CreateCheckbox($sLabel, $iX, $iY)
   GUICtrlCreateCheckbox($sLabel, $iX,  $iY, -1, -1)
   GUICtrlSetOnEvent(-1, "OKTicked")
EndFunc
Func _Exit()
   Exit
EndFunc

I'd imagine these are a bit too simple for what you really want to accomplish, but at least it hopefully gives you some ideas/insight. 
I would think you want some sort of hierarchy when it comes to clicking the checkboxes...like they must be performed in order.  In that case, it would be easy to check if the previous checkbox is checked or not.  You will want to use an array for that to hold the CtrlIds of the checkboxes like Mikah showed.

There are many ways to skin a cat.

edit: Grammar

 

Edited by spudw2k
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

MikahS, spudw2k, 

Huge thanks to both of you for pointing me out these solutions, much appreciated!

The final goal of this GUI is that when the user did the checks and click on the Validate button, the corresponding checkbox will get the value "Done" or "Not Done", (depending if checked or not) and a logfile will be created summing up what has been done or not.

To clarify --> On submit, if ticked, checkbox value = done if not, checkbox value = not done. (I'll also have to write this in a ini file to retrieve the values if needed but it's easy

 

Any idea on how to append this?

Many thanks again!

 

I'd imagine these are a bit too simple for what you really want to accomplish, but at least it hopefully gives you some ideas/insight. 

I would think you want some sort of hierarchy when it comes to clicking the checkboxes...like they must be performed in order.  In that case, it would be easy to check if the previous checkbox is checked or not.  You will want to use an array for that to hold the CtrlIds of the checkboxes like Mikah showed.

There are many ways to skin a cat.

edit: Grammar

 

 

Edit: In fact no, the End Users will not have to perform checkboxes in order. It's kind of a to buy list that can be ticket anytime among what you've found in the store.

And this will be put in a log for us to see what have been done or not. 

Edited by 31290

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You're welcome.  :)

Using the File* functions should work out here, or if you're using a .log file you can use

_FileWriteLog

You can make a button and make a case in the messageloop for the event. When you submit it check the values and write to the file.

Quick example:

#include <GUIConstants.au3>

Local $gui, $array[5], $button, $i

$gui = GUICreate("TEST", 200, 200)
$array[0] = GUICtrlCreateCheckbox("test", 5, 10)
$array[1] = GUICtrlCreateCheckbox("test", 5, 30)
$array[2] = GUICtrlCreateCheckbox("test", 5, 50)
$array[3] = GUICtrlCreateCheckbox("test", 5, 70)
$array[4] = GUICtrlCreateCheckbox("test", 5, 90)
$button = GUICtrlCreateButton("submit", 125, 90)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            For $i = 0 To UBound($array) - 1 Step 1
                If GUICtrlRead($array[$i]) = 1 Then
                    FileWrite(@ScriptDir & "\testlog.txt", "Checkbox " & $i & " value = done" & @CRLF)
                Else
                    FileWrite(@ScriptDir & "\testlog.txt", "Checkbox " & $i & " value = not done" & @CRLF)
                EndIf
            Next
    EndSwitch
WEnd
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Thanks again for your nice tips MikahS :)

This is working well :)

But I do have one more question for you (or anyone that can also help).

I tried to combine what you've share earlier but I can't manage how to grey the boxes when ticket PLUS writing in the log file with the correct name of the checkboxes. I don't know if you know what I mean.

And the all point of this is to write also the state of each boxes in an .ini file in order to create a sort of "retrieve" button that will read the ini file for the user to see what he already done (in case of a reboot or later on for our bosses ^^)

This is quite hard for me as my knowledge of autoit just started last Friday even if my whole script is nearly 600+ lines at this point :D [i've came from Autohoykey to Autoit because of my work ^^)

Huge thanks again for all the help you provide to me but is it possible you help me again ? :D

See you


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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

No problem, can you show us what you have and how you have tried to integrate it?

I would have a look at the

FileRead
GUICtrlSetData

examples in the helpfile to give a better understanding of how you're going to pull text from your file.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I geeked out on this one a bit and made an example  I made a script that generates a form and fills it with checkboxes based on the contents of an INI file.  Upon closing the form the checkbox states are written to the INI.  As a simple security measure the states are hashed in order to deter tampering.  If the hash isn't correct when read the checkbox will not be checked.  The script will also generate the INI upon first run.

#include <GUIConstantsEx.au3>
#include <Crypt.au3>

Opt("GUIOnEventMode", 1)
Global Const $sINIFile = "test.ini"
Global $aCheckboxes

GenGUI()

Func GenGUI()
    Local $ManualActionsGui = GUICreate("Test GUI", 200, 300, -1, -1)
    GUICtrlSetFont(-1, 8.5, 700, 0)

    Local $iX = 10, $iY = 10

    $aCheckboxes = _ReadINI()
    For $x = 0 To UBound($aCheckboxes) - 1
        Local $idCheckBox = _CreateCheckbox($aCheckboxes[$x][0], $iX, $iY)
        If _Crypt_HashData($aCheckboxes[$x][0] & "|" & $sINIFile & "|" & $GUI_CHECKED, $CALG_MD5) = $aCheckboxes[$x][1] Then
            GUICtrlSetState(-1, BitOR($GUI_CHECKED, $GUI_DISABLE))
        EndIf
        $aCheckboxes[$x][0] = $idCheckBox
        $iY += 20
    Next

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $ManualActionsGui)
    GUISetState()

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>GenGUI

Func _Checked()
    If GUICtrlRead(@GUI_CtrlId) = 1 Then GUICtrlSetState(@GUI_CtrlId, $GUI_DISABLE)
EndFunc   ;==>_Checked

Func _CreateCheckbox($sLabel, $iX, $iY)
    Local $idCheckBox = GUICtrlCreateCheckbox($sLabel, $iX, $iY, -1, -1)
    GUICtrlSetOnEvent(-1, "_Checked")

    Return $idCheckBox
EndFunc   ;==>_CreateCheckbox

Func _Exit()
    _WriteINI()
    Exit
EndFunc   ;==>_Exit

Func _GenINIFile()
    Local $aCheckboxes[10] = ["1st Checkbox", "2nd Checkbox", "3rd Checkbox", "4th Checkbox", "5th Checkbox", "6th Checkbox", "7th Checkbox", "8th Checkbox", "9th Checkbox", "10th Checkbox"]
    FileClose(FileOpen($sINIFile, 2))
    For $x = 0 To UBound($aCheckboxes) - 1
        IniWrite($sINIFile, "Checkbox_Labels", $x, $aCheckboxes[$x])
        IniWrite($sINIFile, "Checkbox_States", $x, "")
    Next
EndFunc   ;==>_GenINIFile

Func _ReadINI()
    If Not FileExists($sINIFile) Then _GenINIFile()
    Local $aCheckbox_Labels = IniReadSection($sINIFile, "Checkbox_Labels")
    If @error Then Return SetError(1, 0, 0) ;If INIReadSection Failed Set Error = 1
    Local $aCheckbox_States = IniReadSection($sINIFile, "Checkbox_States")
    If @error Then Return SetError(2, 0, 0) ;If INIReadSection Failed Set Error = 2

    Local $aCheckboxes[$aCheckbox_Labels[0][0]][2]
    For $x = 0 To ($aCheckbox_Labels[0][0] - 1)
        $aCheckboxes[$x][0] = $aCheckbox_Labels[$x + 1][1]
        $aCheckboxes[$x][1] = $aCheckbox_States[$x + 1][1]
    Next

    Return $aCheckboxes
EndFunc   ;==>_ReadINI

Func _WriteINI()
    For $x = 0 To UBound($aCheckboxes) - 1
        IniWrite($sINIFile, "Checkbox_Labels", $x, GUICtrlRead($aCheckboxes[$x][0], 1))
        IniWrite($sINIFile, "Checkbox_States", $x, _Crypt_HashData(GUICtrlRead($aCheckboxes[$x][0], 1) & "|" & $sINIFile & "|" & GUICtrlRead($aCheckboxes[$x][0]), $CALG_MD5))
    Next
EndFunc   ;==>_WriteINI

edit:  I added the INI Filename to the hashing func.  That way multiple INI files could have the same checkboxes used and they won't produce the same hash.

Edited by spudw2k

Share this post


Link to post
Share on other sites

Spudw2k:

What can I say except a BIG BIG THANKS to you. This is exactly what I need!!! Just a few adjustments on my side and this will be the cherry on the cake!  :bike:  :thumbsup:  :huggles:  B)

I have to admit that, at this point of my knowledge, I would never be able to write that. And this is cool because now, I can learn a lot from your script and extend my knowledge.

 

@MikahS: I'd like to thanks you again for all the ideas you pointed out at me, that was also very helpful and I'm now able to extend my knowledge to my scripts, thanks!    o:) :sorcerer:

 


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

Share this post


Link to post
Share on other sites

Anytime.  ^_^

1 person likes this

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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