Jump to content

shorten code for multi checkboxes?


royalmarine
 Share

Recommended Posts

Hi all,

Here's my current code of a small project I'm working on. Is there an easier way of doing these checks?

Ideally I need to be able to tick any of the boxes, and send a button press to the specific window, depending on which boxes are checked.

But with the amount of if, elseIf statements, it's getting long and I think it might be best handled by an array and a for loop if possible?

I've tried several attempts at a for loop and an array, but made a mess of it.. So, I've gone back to my default script below.

Also, is it possible to put the ControlClick details into a variable?

e.g. 

$t1 = test1.map

$t2 = test2.map

and then call it in ControlClick as follows

ControlClick($t1, "" "[NAME:buttonInit]")

 

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

$Form1_1 = GUICreate("Contax Dispenser control", 615, 438, 192, 124)
$Button1 = GUICtrlCreateButton("Init", 8, 8, 123, 41)
$Button2 = GUICtrlCreateButton("Piston in Load Position", 8, 56, 123, 41)
$Button3 = GUICtrlCreateButton("Piston in Dispens", 8, 104, 123, 41)
$Button4 = GUICtrlCreateButton("Start toggle Piston", 8, 152, 123, 41)
$Button5 = GUICtrlCreateButton("Close", 8, 200, 123, 41)
$Button6 = GUICtrlCreateButton("Exit", 8, 248, 123, 41)

$Checkbox1 = GUICtrlCreateCheckbox("Dispenser 1", 240, 16, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Dispenser 2", 240, 48, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Dispenser 3", 240, 80, 97, 17)
$Checkbox4 = GUICtrlCreateCheckbox("Dispenser 4", 240, 112, 97, 17)
$Checkbox5 = GUICtrlCreateCheckbox("Dispenser 5", 240, 144, 97, 17)
$Checkbox6 = GUICtrlCreateCheckbox("Dispenser 6", 240, 176, 97, 17)
$Checkbox7 = GUICtrlCreateCheckbox("Dispenser 7", 240, 208, 97, 17)
$Checkbox8 = GUICtrlCreateCheckbox("Dispenser 8", 240, 240, 97, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED And ($Checkbox4) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")
                ControlClick("test4.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED And ($Checkbox4) = $GUI_CHECKED And ($Checkbox5) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")
                ControlClick("test4.map", "", "[NAME:buttonInit]")
                ControlClick("test5.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED And ($Checkbox4) = $GUI_CHECKED And ($Checkbox5) = $GUI_CHECKED And ($Checkbox6) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")
                ControlClick("test4.map", "", "[NAME:buttonInit]")
                ControlClick("test5.map", "", "[NAME:buttonInit]")
                ControlClick("test6.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED And ($Checkbox4) = $GUI_CHECKED And ($Checkbox5) = $GUI_CHECKED And ($Checkbox6) = $GUI_CHECKED And ($Checkbox7) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")
                ControlClick("test4.map", "", "[NAME:buttonInit]")
                ControlClick("test5.map", "", "[NAME:buttonInit]")
                ControlClick("test6.map", "", "[NAME:buttonInit]")
                ControlClick("test7.map", "", "[NAME:buttonInit]")

            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED And ($Checkbox2) = $GUI_CHECKED And ($Checkbox3) = $GUI_CHECKED And ($Checkbox4) = $GUI_CHECKED And ($Checkbox5) = $GUI_CHECKED And ($Checkbox6) = $GUI_CHECKED And ($Checkbox7) = $GUI_CHECKED And ($Checkbox8) = $GUI_CHECKED Then

                Sleep(2000)
                ControlClick("test1.map", "", "[NAME:buttonInit]")
                ControlClick("test2.map", "", "[NAME:buttonInit]")
                ControlClick("test3.map", "", "[NAME:buttonInit]")
                ControlClick("test4.map", "", "[NAME:buttonInit]")
                ControlClick("test5.map", "", "[NAME:buttonInit]")
                ControlClick("test6.map", "", "[NAME:buttonInit]")
                ControlClick("test7.map", "", "[NAME:buttonInit]")
                ControlClick("test8.map", "", "[NAME:buttonInit]")

            Else
                MsgBox($MB_SYSTEMMODAL, "Warning", "You must select at least one dispenser.", 10)

            EndIf

        Case $Button2
        Case $Button3
        Case $Button4
        Case $Button5
        Case $Button6
            Exit

    EndSwitch
WEnd

 

Link to comment
Share on other sites

Something like this?

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

Global $iDispensers = 8, $aCheckbox[$iDispensers], $iCountChecked = 0
$Form1_1 = GUICreate("Contax Dispenser control", 615, 438, 192, 124)
$Button1 = GUICtrlCreateButton("Init", 8, 8, 123, 41)
$Button2 = GUICtrlCreateButton("Piston in Load Position", 8, 56, 123, 41)
$Button3 = GUICtrlCreateButton("Piston in Dispens", 8, 104, 123, 41)
$Button4 = GUICtrlCreateButton("Start toggle Piston", 8, 152, 123, 41)
$Button5 = GUICtrlCreateButton("Close", 8, 200, 123, 41)
$Button6 = GUICtrlCreateButton("Exit", 8, 248, 123, 41)

For $i = 0 To $iDispensers - 1
    $aCheckbox[$i] = GUICtrlCreateCheckbox("Dispenser " & $i + 1, 240, ($i * 32) + 16, 97, 17)
Next
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            For $i = 0 To UBound($aCheckbox, 1) - 1
                If _IsChecked($aCheckbox[$i]) Then
                    $iCountChecked += 1
                    Sleep(2000)
                    ControlClick("test" & $i + 1 & ".map", "", "[NAME:buttonInit]")
                EndIf
            Next
            If $iCountChecked = 0 Then MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONWARNING), "Warning", "You must select at least one dispenser.", 10)
        Case $Button2
        Case $Button3
        Case $Button4
        Case $Button5
        Case $Button6
    EndSwitch
WEnd

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Glad to be of service :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

One quick one Water,

 

If I wanted to add another checkbox to check / uncheck all the other checkboxes, would something like this do it?

I assume not.. I probably need a new function to call on the GuiCtrlGetState of the current box?

 

$CheckboxAll = GUICtrlCreateCheckbox("Enable all", 240, 272, 97, 17)

Case $CheckboxAll
           For $i = 0 To UBound($aCheckbox, 1) - 1
                If _IsChecked($aCheckbox[$i]) Then
                    $iCountChecked += 1
                    GUICtrlSetState ($aCheckbox, $GUI_CHECKED)
                EndIf
            Next

 

Link to comment
Share on other sites

With the following modification you can enable/disable all other checkboxes:

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

Global $iDispensers = 8, $aCheckbox[$iDispensers], $iCountChecked = 0
$Form1_1 = GUICreate("Contax Dispenser control", 615, 438, 192, 124)
$Button1 = GUICtrlCreateButton("Init", 8, 8, 123, 41)
$Button2 = GUICtrlCreateButton("Piston in Load Position", 8, 56, 123, 41)
$Button3 = GUICtrlCreateButton("Piston in Dispens", 8, 104, 123, 41)
$Button4 = GUICtrlCreateButton("Start toggle Piston", 8, 152, 123, 41)
$Button5 = GUICtrlCreateButton("Close", 8, 200, 123, 41)
$Button6 = GUICtrlCreateButton("Exit", 8, 248, 123, 41)
For $i = 0 To $iDispensers - 1
    $aCheckbox[$i] = GUICtrlCreateCheckbox("Dispenser " & $i + 1, 240, ($i * 32) + 16, 97, 17)
Next
$CheckboxAll = GUICtrlCreateCheckbox("Enable/Disable all", 240, 272, 110, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CheckboxAll
            $bCheckAll = _IsChecked($CheckboxAll)
            For $i = 0 To UBound($aCheckbox, 1) - 1
                If $bCheckAll Then
                    GUICtrlSetState($aCheckbox[$i], $GUI_CHECKED)
                Else
                    GUICtrlSetState($aCheckbox[$i], $GUI_UNCHECKED)
                EndIf
            Next
        Case $Button1
            For $i = 0 To UBound($aCheckbox, 1) - 1
                If _IsChecked($aCheckbox[$i]) Then
                    $iCountChecked += 1
                    Sleep(2000)
                    ControlClick("test" & $i + 1 & ".map", "", "[NAME:buttonInit]")
                EndIf
            Next
            If $iCountChecked = 0 Then MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONWARNING), "Warning", "You must select at least one dispenser.", 10)
        Case $Button2
        Case $Button3
        Case $Button4
        Case $Button5
        Case $Button6
    EndSwitch
WEnd

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...