Jump to content

CheckBox strange behaviour


stick3r
 Share

Recommended Posts

Hi, I have this script and I need to reset all checkboxes to UNCHECKED when button is pressed.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
Global $Form = GUICreate("Checklist", 190, 407, 1233, 178)
Global $text = ""
Global $CheckBox
GUICtrlCreateTab(1, 1, 190, 365)
GUICtrlCreateTabItem("TAB 1")
Global $InputCaseID1 = GUICtrlCreateInput($text, 80, 25, 80, 21)
Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18)
Global $CheckBox1 = GUICtrlCreateCheckbox("1111111", 8, 48, 110, 17)
Global $CheckBox2 = GUICtrlCreateCheckbox("2222222", 8, 72, 110, 17)
Global $CheckBox3 = GUICtrlCreateCheckbox("333333333", 8, 96, 110, 17)
Global $CheckBox4 = GUICtrlCreateCheckbox("444444444", 8, 120, 110, 17)
Global $CheckBox5 = GUICtrlCreateCheckbox("55555555", 8, 144, 110, 17)
Global $CheckBox6 = GUICtrlCreateCheckbox("666666666", 8, 168, 110, 17)
Global $CheckBox7 = GUICtrlCreateCheckbox("77777777", 8, 192, 150, 17)
Global $CheckBox8 = GUICtrlCreateCheckbox("888888888", 8, 216, 150, 17)
Global $CheckBox9 = GUICtrlCreateCheckbox("99999999", 8, 240, 110, 17)
Global $CheckBox10 = GUICtrlCreateCheckbox("45646", 8, 264, 110, 17)
Global $CheckBox11 = GUICtrlCreateCheckbox("4345634", 8, 288, 97, 17)
Global $CheckBox12 = GUICtrlCreateCheckbox("4563456", 8, 312, 97, 17)
Global $CheckBox13 = GUICtrlCreateCheckbox("456456", 8, 336, 97, 17)

GUICtrlCreateTabItem("TAB 2")
Global $InputCaseID2 = GUICtrlCreateInput("", 80, 25, 80, 21)
Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18)
Global $CheckBox14 = GUICtrlCreateCheckbox("AAAAAAA", 8, 48, 110, 17)
Global $CheckBox15 = GUICtrlCreateCheckbox("PPPPPPPPP", 8, 72, 110, 17)
Global $CheckBox16 = GUICtrlCreateCheckbox("BBBBBBBB", 8, 96, 110, 17)
Global $CheckBox17 = GUICtrlCreateCheckbox("CCCCCCCCCCCCC", 8, 120, 110, 17)
Global $CheckBox18 = GUICtrlCreateCheckbox("DDDDDDDDDDDDD", 8, 144, 110, 17)
Global $CheckBox19 = GUICtrlCreateCheckbox("EEEEEEEEEE", 8, 168, 110, 17)
Global $CheckBox20 = GUICtrlCreateCheckbox("FFFFFFFFFFF", 8, 192, 150, 17)
Global $CheckBox21 = GUICtrlCreateCheckbox("GGGGGGGGGG", 8, 216, 150, 17)
Global $CheckBox22 = GUICtrlCreateCheckbox("HHHHHHHHH", 8, 240, 110, 17)
Global $CheckBox23 = GUICtrlCreateCheckbox("IIIIIIIII", 8, 264, 110, 17)
Global $CheckBox24 = GUICtrlCreateCheckbox("JJJJJJJ", 8, 288, 97, 17)
Global $CheckBox25 = GUICtrlCreateCheckbox("KKKKKKKKKKK", 8, 312, 97, 17)
Global $CheckBox26 = GUICtrlCreateCheckbox("LLLLLLLLL", 8, 336, 97, 17)

GUICtrlCreateTabItem("")
Global $CheckBoxAlwaysOnTop = GUICtrlCreateCheckbox("Always on Top", 8, 380, 97, 17)
Global $btnReset = GUICtrlCreateButton("RESET", 104, 365, 75, 41)
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CheckBoxAlwaysOnTop
            _SetAlwaysOnTop()
        Case $btnReset
        For $i = 7 To 19 Step 1 ;Remove checked CheckBoxes
            GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED)
        Next
        For $i = 23 To 35 Step 1
            GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED)
        Next
        GUICtrlSetData($InputCaseID1, "")
        GUICtrlSetData($InputCaseID2, "")
    EndSwitch
WEnd

Func _SetAlwaysOnTop()
    If GUICtrlRead($CheckBoxAlwaysOnTop) = $GUI_CHECKED Then
        WinSetOnTop($Form, "", 1) ;On top
    Else
        WinSetOnTop($Form, "", 0) ;Not on top
    EndIf
EndFunc   ;==>_SetAlwaysOnTop

My question is:

Why this  For loop for GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED) does not work properly

I have $CheckBox1, $CheckBox2.......till $CheckBox26 and with For $i = 1 To 26 it does not work. It only unchecks few of them, but not all.

Later I have discovered that For $i = 7 To 19 removes  $CheckBox1 to  $CheckBox13 and For $i = 23 To 35 removes  $CheckBox14 to  $CheckBox26

Any idea why?

Link to comment
Share on other sites

  • Moderators

stick3r,

AutoIt uses an array to store the ControlIDs of its controls (the value returned by the GUICtrlCreate* call) and these ControlIDs are actually the index number of the control in that array. If you look at the actual values returned, you will see that they are in numerical order of creation, as you can see if you add these lines to your script:

Global $CheckBox13 = GUICtrlCreateCheckbox("456456", 8, 336, 97, 17)

GUICtrlCreateTabItem("TAB 2")
Global $InputCaseID2 = GUICtrlCreateInput("", 80, 25, 80, 21)
Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18)
Global $CheckBox14 = GUICtrlCreateCheckbox("AAAAAAA", 8, 48, 110, 17)

MsgBox(0, "ControlIDs", "$Checkbox13: " & @TAB & $CheckBox13 & @CRLF & _
                                      "TAB2 goes here" & @CRLF & _
                                      "$InputCaseID2: " & @TAB & $InputCaseID2 & @CRLF & _
                                      "$lblCaseID:" & @TAB & $lblCaseID & @CRLF & _
                                      "$CheckBox14: " & @TAB & $CheckBox14)

Global $CheckBox15 = GUICtrlCreateCheckbox("PPPPPPPPP", 8, 72, 110, 17)

I would suggest using arrays to hold the returned ControlIDs - that way life is very easy:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

; Create an array to hold the returned ControlIDs <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $aCheckbox[27] ; Why 27? Because arrays start at 0

#Region ### START Koda GUI section ### Form=
    Global $Form = GUICreate("Checklist", 190, 407, 1233, 178)
    Global $text = ""
    Global $aCheckbox
    GUICtrlCreateTab(1, 1, 190, 365)
    GUICtrlCreateTabItem("TAB 1")
    Global $InputCaseID1 = GUICtrlCreateInput($text, 80, 25, 80, 21)
    Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18)
    ; Store the ControlIDs as the controls are created <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $aCheckbox[1] = GUICtrlCreateCheckbox("1111111", 8, 48, 110, 17)
    $aCheckbox[2] = GUICtrlCreateCheckbox("2222222", 8, 72, 110, 17)
    $aCheckbox[3] = GUICtrlCreateCheckbox("333333333", 8, 96, 110, 17)
    $aCheckbox[4] = GUICtrlCreateCheckbox("444444444", 8, 120, 110, 17)
    $aCheckbox[5] = GUICtrlCreateCheckbox("55555555", 8, 144, 110, 17)
    $aCheckbox[6] = GUICtrlCreateCheckbox("666666666", 8, 168, 110, 17)
    $aCheckbox[7] = GUICtrlCreateCheckbox("77777777", 8, 192, 150, 17)
    $aCheckbox[8] = GUICtrlCreateCheckbox("888888888", 8, 216, 150, 17)
    $aCheckbox[9] = GUICtrlCreateCheckbox("99999999", 8, 240, 110, 17)
    $aCheckbox[10] = GUICtrlCreateCheckbox("45646", 8, 264, 110, 17)
    $aCheckbox[11] = GUICtrlCreateCheckbox("4345634", 8, 288, 97, 17)
    $aCheckbox[12] = GUICtrlCreateCheckbox("4563456", 8, 312, 97, 17)
    $aCheckbox[13] = GUICtrlCreateCheckbox("456456", 8, 336, 97, 17)

    GUICtrlCreateTabItem("TAB 2")
    Global $InputCaseID2 = GUICtrlCreateInput("", 80, 25, 80, 21)
    Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18)
    $aCheckbox[14] = GUICtrlCreateCheckbox("AAAAAAA", 8, 48, 110, 17)
    $aCheckbox[15] = GUICtrlCreateCheckbox("PPPPPPPPP", 8, 72, 110, 17)
    $aCheckbox[16] = GUICtrlCreateCheckbox("BBBBBBBB", 8, 96, 110, 17)
    $aCheckbox[17] = GUICtrlCreateCheckbox("CCCCCCCCCCCCC", 8, 120, 110, 17)
    $aCheckbox[18] = GUICtrlCreateCheckbox("DDDDDDDDDDDDD", 8, 144, 110, 17)
    $aCheckbox[19] = GUICtrlCreateCheckbox("EEEEEEEEEE", 8, 168, 110, 17)
    $aCheckbox[20] = GUICtrlCreateCheckbox("FFFFFFFFFFF", 8, 192, 150, 17)
    $aCheckbox[21] = GUICtrlCreateCheckbox("GGGGGGGGGG", 8, 216, 150, 17)
    $aCheckbox[22] = GUICtrlCreateCheckbox("HHHHHHHHH", 8, 240, 110, 17)
    $aCheckbox[23] = GUICtrlCreateCheckbox("IIIIIIIII", 8, 264, 110, 17)
    $aCheckbox[24] = GUICtrlCreateCheckbox("JJJJJJJ", 8, 288, 97, 17)
    $aCheckbox[25] = GUICtrlCreateCheckbox("KKKKKKKKKKK", 8, 312, 97, 17)
    $aCheckbox[26] = GUICtrlCreateCheckbox("LLLLLLLLL", 8, 336, 97, 17)

    GUICtrlCreateTabItem("")
    Global $aCheckboxAlwaysOnTop = GUICtrlCreateCheckbox("Always on Top", 8, 380, 97, 17)
    Global $btnReset = GUICtrlCreateButton("RESET", 104, 365, 75, 41)
    GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aCheckboxAlwaysOnTop
            _SetAlwaysOnTop()
        Case $btnReset
            ; Now you can loop through them in a single loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            For $i = 1 To 26 ;Remove checked CheckBoxes
                GUICtrlSetState($aCheckbox[$i], $GUI_UNCHECKED)
            Next
            GUICtrlSetData($InputCaseID1, "")
            GUICtrlSetData($InputCaseID2, "")
    EndSwitch
WEnd

Func _SetAlwaysOnTop()
    If GUICtrlRead($aCheckboxAlwaysOnTop) = $GUI_CHECKED Then
        WinSetOnTop($Form, "", 1) ;On top
    Else
        WinSetOnTop($Form, "", 0) ;Not on top
    EndIf
EndFunc   ;==>_SetAlwaysOnTop

Please ask if you have any questions.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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