Sign in to follow this  
Followers 0
lgwapnitsky

Watching checkboxes from an array

2 posts in this topic

I am creating a utility for my company that reads in a list of servers from a text file and generates checkboxes based on that information. I would like to select particular servers in which to apply information, but am unsure of how to read multiple checkboxes in a variable size array. I've attached my code for review, and am very happy to answer any questions that can clear possible confusion.

Regards,

Larry

Func _Main()
    ReadINI()
    
    GuiCreate("File Copier", 800, 300)

    $SelectFilesButton = GUICtrlCreateButton("Select File(s)", 15, 250, 115, 30)
    $RemoveFilesButton = GUICtrlCreateButton("Remove Selection(s)", 140, 250, 115, 30)
    $CopyFilesButton = GUICtrlCreateButton("Copy File(s)", 265, 250, 115, 30)

    GUICtrlCreateLabel("Selected Files",50,15)
    $FileList = GUICtrlCreateList("", 50, 35, 300,200, BitOr($LBS_EXTENDEDSEL, $WS_VSCROLL, $WS_HSCROLL))
    
    $numBox[0] = GUICtrlCreateCheckbox("Select All", 450, 30, 100, 30)
    
    $newRow = 0
    $yLoc = 20
    $yLocInc = 40
    
    for $i = 0 to UBOUND($arrDestinations) - 1
        if Mod($newRow, 3) = 0 then
            $yLoc = $yLoc + $yLocInc
        EndIf
        $checkbox = GUICtrlCreateCheckbox($arrDestinations[$i], 450 + (100 * Mod($i, 3)), $yLoc, 100, 50);, $BS_AUTO3STATE)
        _ArrayAdd($numBox, $checkbox)
        $newRow += 1
    next
    
    GUISetState()
    
    While 1
        $GUIAction = GuiGetMsg()
        Switch $GUIAction
            Case $GUI_EVENT_CLOSE
                ExitLoop ; closes the GUI
            Case $SelectFilesButton
                _GUICtrlButton_Enable($SelectFilesButton, False)
                SelectFiles()
                _GUICtrlButton_Enable($SelectFilesButton)
            Case $RemoveFilesButton
                RemoveFiles()
            Case $CopyFilesButton
                CopyFiles()
            ; Case $numBox[0]
                ; Select
                    ; Case (_GUICtrlButton_GetCheck($numBox[0]) = 1)
                        ; for $i = 1 to UBOUND($arrDestinations)
                            ; GUICtrlSetState($numbox[$i], ($GUI_CHECKED + $GUI_DISABLE))
                        ; next
                    ; Case (_GUICtrlButton_GetCheck($numBox[0]) = 0)
                        ; for $i = 1 to UBOUND($arrDestinations)
                            ; GUICtrlSetState($numbox[$i], ($GUI_UNCHECKED + $GUI_ENABLE))
                        ; next
                ; EndSelect
        EndSwitch
    WEnd
EndFunc

Share this post


Link to post
Share on other sites



lgwapnitsky,

A few changes to the script:

1. Presize the array depending on how many items you have read into the $arrDestinations array.

2. Presize the GUI to fit all the checkboxes.

3. Use the built-in GUICtrlSetState rather than _GUICtrlButton_Enable.

4. Use GUICtrlRead in both basic and advanced forms to get the check state and text for the checkboxes.

Look for the <<<<<<<<<<<<<<<<<<< lines:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListBoxConstants.au3>

#include <File.au3>
#include <Array.au3>

_Main()

Func _Main()

    $newRow = 0
    $yLoc = 20
    $yLocInc = 40

    ;ReadINI()
    $arrDestinations = _FileListToArray("M:\Program\Au3 Scripts", "*", 2) ; Simulate reading in data

    ; Size array
    Local $numbox[UBound($arrDestinations)] ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ; Determine GUI height
    $GUI_Height = $yLoc + $yLocInc * (2 + Int(UBound($arrDestinations) / 3)) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    GUICreate("File Copier", 800, $GUI_Height)

    $SelectFilesButton = GUICtrlCreateButton("Select File(s)", 15, 250, 115, 30)
    $RemoveFilesButton = GUICtrlCreateButton("Remove Selection(s)", 140, 250, 115, 30)
    $CopyFilesButton = GUICtrlCreateButton("Copy File(s)", 265, 250, 115, 30)

    GUICtrlCreateLabel("Selected Files", 50, 15)
    $FileList = GUICtrlCreateList("", 50, 35, 300, 200, BitOR($LBS_EXTENDEDSEL, $WS_VSCROLL, $WS_HSCROLL))

    $numbox[0] = GUICtrlCreateCheckbox("Select All", 450, 30, 100, 30)

    For $i = 1 To UBound($arrDestinations) - 1
        If Mod($newRow, 3) = 0 Then
            $yLoc = $yLoc + $yLocInc
        EndIf
        ; Save checkbox COntrolIDs
        $numbox[$i] = GUICtrlCreateCheckbox($arrDestinations[$i], 450 + (100 * Mod($i, 3)), $yLoc, 100, 50);, $BS_AUTO3STATE)
        ;_ArrayAdd($numbox, $checkbox)
        $newRow += 1
    Next

    _ArrayDisplay($numbox)

    GUISetState()

    While 1
        $GUIAction = GUIGetMsg()
        Switch $GUIAction
            Case $GUI_EVENT_CLOSE
                ExitLoop ; closes the GUI
            Case $SelectFilesButton
                GUICtrlSetState($SelectFilesButton, $GUI_DISABLE) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                ;SelectFiles()
                GUICtrlSetState($SelectFilesButton, $GUI_ENABLE) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            Case $RemoveFilesButton
                ;RemoveFiles()
            Case $CopyFilesButton
                
                ;CopyFiles() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                For $i = 0 To UBound($numbox) - 1
                    ; Read check state
                    If GUICtrlRead($numbox[$i]) = 1 Then
                        ; Read text
                        MsgBox(0, GUICtrlRead($numbox[$i], 1),  "Checked")
                    Else
                        MsgBox(0, GUICtrlRead($numbox[$i], 1),  "Unchecked")
                    EndIf
                Next

                ; Case $numBox[0]
                ; Select
                ; Case (_GUICtrlButton_GetCheck($numBox[0]) = 1)
                ; for $i = 1 to UBOUND($arrDestinations)
                ; GUICtrlSetState($numbox[$i], ($GUI_CHECKED + $GUI_DISABLE))
                ; next
                ; Case (_GUICtrlButton_GetCheck($numBox[0]) = 0)
                ; for $i = 1 to UBOUND($arrDestinations)
                ; GUICtrlSetState($numbox[$i], ($GUI_UNCHECKED + $GUI_ENABLE))
                ; next
                ; EndSelect
        EndSwitch
    WEnd
EndFunc   ;==>_Main

Please ask if anything is unclear. :(

M23

P.S. It greatly increases your chances of getting a helpful reply if you post code which runs and does not need a great deal of added code to even get the GUI to appear or to replace missing functions. :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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