Sign in to follow this  
Followers 0
RogerH

Clear multiple checkboxes at one keypress and save GUI settings to registry or ini file

10 posts in this topic

Good Afternoon all,

I have been searching and haven't had too much luck finding a good solution for these items

I have a GUI script I've built that has a bunch of checkboxes and input boxes. I put a button on to load the defaults, and another one to clear all of them.

Initially I did this:

Func _Clear_All()
    ;clear all check boxes
    For $i = 10 To 66
        GUICtrlSetState($i, $GUI_UNCHECKED)
    Next
    GUICtrlSetData($in_tccount, "")
    GUICtrlSetData($in_op_count, "")
    GUICtrlSetData($in_system_name, "")
    GUICtrlSetData($in_pl_count, "")
EndFunc   ;==>_Clear_Checkboxes

But if I modify the UI (which I have been doing a bunch) it changes the id of the checkboxes, and that FOR loop may or may not get all of them.

So it raises a few questions, which also relates to saving off all the window info into the registry or an .ini file.

1) is there a "for each" type of loop that will iterate through all of the elements on a GUI form?

2) is GUICtrlSetState($i, $GUI_UNCHECKED) smart enough not to throw an error if I call it on an input-box for example?

a) If it's not, how do I check what type of GUI element the control is?

Share this post


Link to post
Share on other sites



I doubt that code you posted works at all

You say your controls have ids of 10 - 66, I thought you could not set the ID of a control

And if you are using handles then 10 - 66 wont work either

You are going to have to give your controls variables, perhaps in an array even, and posting a bit of your code showing how you are creating the gui will get you some help a lot quicker.

Good luck


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Setting $GUI_UNCHECKED Will only afect Radio and Checkbox controls.

Assign each checkbox to a variable then just put it in a loop.

For $i = $Cbx_1 To $Cbx_6
    GUICtrlSetState($i, $GUI_UNCHECKED)
Next

If you are worried about affectin other controls the just move your checkoxes (in your code) so they appear sequentially. This will work fine as long as you are using the default "GUICoordMode" which meas you are NOT using relative coordinates.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

When you build out controls, they are sequentially assigned id's as you add them to the form; so the first control in the code is 1... etc...

I had this working when I grouped all the checkboxes together.

I doubt that code you posted works at all

You say your controls have ids of 10 - 66, I thought you could not set the ID of a control

And if you are using handles then 10 - 66 wont work either

You are going to have to give your controls variables, perhaps in an array even, and posting a bit of your code showing how you are creating the gui will get you some help a lot quicker.

Good luck

Share this post


Link to post
Share on other sites

I've assigned all the checkboxes variables, but as you see from the numbering I'm at 55 of them so far...

Your suggestion for clustering them was how I'd worked it before... I was hoping for something more elegant than that though.

Your loop should work well with the sequential checkboxes, and you've answered my question about the "stuff in the middle"... Thanks!

Setting $GUI_UNCHECKED Will only afect Radio and Checkbox controls.

Assign each checkbox to a variable then just put it in a loop.

For $i = $Cbx_1 To $Cbx_6
    GUICtrlSetState($i, $GUI_UNCHECKED)
Next

If you are worried about affectin other controls the just move your checkoxes (in your code) so they appear sequentially. This will work fine as long as you are using the default "GUICoordMode" which meas you are NOT using relative coordinates.

Share this post


Link to post
Share on other sites

I went ahead and implemented GeoSoft's suggestion -- just loop through ALL controls for the SetState and it worked fine. However, when I did GUICtrlSetData, things got ugly -- all the text for the labels and buttons disappeared.

I removed a few boxes, and cleaned up the interface, so right now I have 50 controls on the form:

My current refinement/process is as follows:

Func _Clear_All()
    ;clear all check boxes
    For $i = 1 To 50
        GUICtrlSetState($i, $GUI_UNCHECKED)
    Next
    For $i = 42 To 45
        GUICtrlSetData($i, "")
    Next

EndFunc   ;

What I did to try keep them grouped nicely is Excel and good naming practices:

$btn_.... - button

$cb_..... - checkbox

$in_..... - input box

$lbl_.... - label

$rad_.... - radio button

This doesn't quite work out as hoped, because there's something else at play in generating the id than just the order -- I get two checkbox labels wiped out instead of my input boxes with this code sample. As another issue, my tab order is pretty significantly messed up as well, as it appears that tab order is controlled by the order of the controls.

What also isn't pretty is the GUICtrlSetState(-1, $GUI_CHECKED) that are put in to set default turned-on items.. this process dumps them all at the end, and eliminates that functionality.

To address that I made a Func _Set_Defaults() that turns on my checkboxes, and sets my default input box values:

Func _Set_Defaults()
    GUICtrlSetState($rad_1, $GUI_CHECKED)
    GUICtrlSetState($cb_1, $GUI_CHECKED)
    GUICtrlSetState($cb_2, $GUI_CHECKED)
    GUICtrlSetState($cb_3, $GUI_CHECKED)
    GUICtrlSetState($cb_4, $GUI_CHECKED)
    GUICtrlSetState($cb_5, $GUI_CHECKED)
    GUICtrlSetState($cb_6, $GUI_CHECKED)
    GUICtrlSetState($cb_7, $GUI_CHECKED)
    GUICtrlSetState($cb_8, $GUI_CHECKED)
    GUICtrlSetState($cb_9, $GUI_CHECKED)
    GUICtrlSetState($cb_10, $GUI_CHECKED)
    GUICtrlSetState($cb_11, $GUI_CHECKED)
    GUICtrlSetState($cb_12, $GUI_CHECKED)
    GUICtrlSetState($cb_13, $GUI_CHECKED)
    GUICtrlSetState($cb_14, $GUI_CHECKED)
    GUICtrlSetData($in_1, "1")
    GUICtrlSetData($in_2, "2")
    GUICtrlSetData($in_3, @ComputerName)
    GUICtrlSetData($in_4, "1")
EndFunc   ;==>_SetDefaults

I put this line in immediately before my GUISetState(@SW_SHOW).

Share this post


Link to post
Share on other sites

Hence the reason for putting all the Checkbox controls sequentially in your script

$Cbx1 = CUICtrlCreateCheckbox(....

;; More checkboxes here

$Cbx8 = CUICtrlCreateCheckbox(....

;; Now put all the controls where you want to empty the text here

$Ctrl_1 = GUICtrlCreate......(......

;; More controls

$Ctrl_10 = GUICtrlCreate......(......

Then 2 Loops

For $i = $Cbx1 To $Cbx8

;; Set state here

Next

For $i = $Ctrl_1 To $Ctrl_10

;; Set Data here

Next


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

If you don't want to use a sequential method then there is another way to do it using the classname but you will have to #include winapi.au3

The function you want is _WinAPI_GetClassName

In your existing loop where you are setting the control to empty data, add a conditional

If _WinAPI_GetClassName($i) <> "Button" Then GUICtrlSetData($i, "")


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Hi, here is my idea for it.

ControlID are added to a delimetered string and then split into an array at the end of a group, controls can be added/taken away easily without messing up.

Button1 Alternates between selectAll/UnSelectAll and uses the arrays of controlID in a loop to set the state.

Button2 Loads the defaults saved in an ini file.

Button3 Saves the defaults to the ini file.

Once the defaults have been saved to the ini file the defaults will be set on starting the script again, if you want the form blank just comment out line 'If FileExists($hGuiCfg) Then _SetDefaults()'

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=

Global $hGuiCfg = @ScriptDir & "\GuiDefaults.cfg"

$Form1 = GUICreate("Form1", 241, 184, 192, 124)
$sChkBxs = GUICtrlCreateCheckbox("Checkbox1", 8, 8, 97, 17)
$sChkBxs &= "|" & GUICtrlCreateCheckbox("Checkbox2", 8, 24, 97, 17)
$sChkBxs &= "|" & GUICtrlCreateCheckbox("Checkbox3", 8, 40, 97, 17)
$sChkBxs &= "|" & GUICtrlCreateCheckbox("Checkbox4", 8, 56, 97, 17)
$sChkBxs &= "|" & GUICtrlCreateCheckbox("Checkbox5", 8, 72, 97, 17)
$aChkBxGrp1 = StringSplit($sChkBxs, "|")

$sInps = GUICtrlCreateInput("", 112, 8, 121, 21)
$sInps &= "|" & GUICtrlCreateInput("", 112, 32, 121, 21)
$sInps &= "|" & GUICtrlCreateInput("", 112, 56, 121, 21)
$sInps &= "|" & GUICtrlCreateInput("", 112, 80, 121, 21)
$sInps &= "|" & GUICtrlCreateInput("", 112, 104, 121, 21)
$aInps = StringSplit($sInps, "|")

$Button1 = GUICtrlCreateButton("Clear all", 13, 144, 70, 25)
$Button2 = GUICtrlCreateButton("Defaults", 85, 144, 70, 25)
$Button3 = GUICtrlCreateButton("Save", 157, 144, 70, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If FileExists($hGuiCfg) Then _SetDefaults()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Switch GUICtrlRead($Button1)
                Case "Select all"
                    For $i = 1 To $aChkBxGrp1[0] Step 1
                        GUICtrlSetState($aChkBxGrp1[$i], $GUI_CHECKED)
                    Next
                    GUICtrlSetData($Button1, "UnSelect All")
                Case "UnSelect All"
                    For $i = 1 To $aChkBxGrp1[0] Step 1
                        GUICtrlSetState($aChkBxGrp1[$i], $GUI_UNCHECKED)
                    Next
                    GUICtrlSetData($Button1, "Select all")
            EndSwitch
        Case $Button2
            _SetDefaults()
        Case $Button3
            If FileExists($hGuiCfg) Then FileDelete($hGuiCfg)
            For $i = 1 To $aChkBxGrp1[0] Step 1
                $fState = BitAND(GUICtrlRead($aChkBxGrp1[$i]), $GUI_CHECKED)
                IniWrite($hGuiCfg, "$aChkBxGrp1", $i, $fState)
            Next
            For $i = 1 To $aInps[0] Step 1
                IniWrite($hGuiCfg, "$aInps", $i, GUICtrlRead($aInps[$i]))
            Next
    EndSwitch
WEnd

Func _SetDefaults()
    For $i = 1 To $aChkBxGrp1[0] Step 1
        $fState = IniRead($hGuiCfg, "$aChkBxGrp1", $i, 0)
        If $fState = 1 Then
            GUICtrlSetState($aChkBxGrp1[$i], $GUI_CHECKED)
        Else
            GUICtrlSetState($aChkBxGrp1[$i], $GUI_UNCHECKED)
        EndIf
    Next
    For $i = 1 To $aInps[0] Step 1
        GUICtrlSetData($aInps[$i], IniRead($hGuiCfg, "$aInps", $i, ""))
    Next
    GUICtrlSetData($Button1, "UnSelect All")
EndFunc   ;==>_SetDefaults

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

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