Jump to content

Dynamic Checkboxes in a GUI

Recommended Posts



I have used this forum to get help many times. I thought it was time to (hopefully) help others.

I created a script that reads a text file with a list of videos, displays the videos in a GUI with checkboxes next to the names, and displays the selected videos. This will become a part of a larger script I am creating to test a video player. The tough part for me was creating the GUI and Dynamic list of videos. I had a lot of trouble finding samples to help me, but finally found one  written by Melba23. The link is in the code, so he gets credit for helping! I also have not used arrays much and they are very picky about looping through the arrays without getting the dreaded error " Array variable has incorrect number of subscripts or subscript dimension range exceeded."  However diligence paid off!

To run this code, take the video names commented below and create a videos.txt file in your script execution directory. You can put however many video names in this list. Thus the dynamic features of the code.



#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>

Local $sMediaFile = @ScriptDir & "\videos.txt"
;~ Videos in videos.txt are:
;~ bbb_1080_60s.mp4
;~ bbb_1080_60s_1.mp4
;~ bbb_1080_60s_2.mp4
;~ tos_4K_60s_HEVC.mp4
;~ tos_4K_60s_HEVC_1.mp4
;~ tos_4K_60s_HEVC_2.mp4
;~ Additional videos can be added to this list. The functions are Dynamic.
Dim $aMediaManifest
Local $aArrayFile
Local $aVideos
Local $sVideoName
Local $i

; Put the Video File Names into an Array
_FileReadToArray($sMediaFile, $aArrayFile)
Local $iVideoCount = UBound($aArrayFile) -1 ; Get the number of videos - 1 to prevent errors

_ArrayDelete($aArrayFile, 0) ;Counter just gets in the way
; Move backwards through the array deleting the blank lines
For $i = $iVideoCount - 1 To 0 Step -1
    If $aArrayFile[$i] = "" Then
        _ArrayDelete($aArrayFile, $i)

$aVideos = DisplayVideos($aArrayFile)
$iVideoCount = UBound($aArrayFile) -1

_ArrayDisplay($aVideos) ; Display the checked videos
;~ End of MAIN
;   GUI to display Videos in checkboxes
Func DisplayVideos($aArrayFile)
    Local $iTop = -1
    Local $iLeft = -1
    Local $iWidth
    Local $iHeight = $iVideoCount * 30
    Local $iL = 10
    Local $iT = 10
    Local $iRow = 0
    Local $aVideo
    Local $iA = 0, $iB = 0
    Local $sFill = ""
    $iMMCount = UBound($aArrayFile)
    $iMMNewCount = $iMMCount - 1
    Local $aGUICheckbox[$iMMCount]
    Local $aCheckedVideos[$iMMCount]

    ; Put the Video File Names into an Array
    $hGUI = GUICreate("Video Checkbox", $iLeft, $iTop, $iWidth, $iHeight)
    GUICtrlCreateLabel("Videos", 180, $iT)
    $iT = $iT + 30

    ; This is a great example of using arrays to create GUI check boxes or radio buttons
    For $i = 0 To $iMMNewCount Step 1
        $sMP4Text = $aArrayFile[$i]
        $aGUICheckbox[$i] = GUICtrlCreateCheckbox($sMP4Text, 30, $iT)
        $iT += 30

    $idClose1 = GUICtrlCreateButton("Start", $iL, $iT)

    ; This section reads the checkboxes and puts the video names in an array in their original position
    ;   in case this is important (as it is to me)
    ; This was the toughest part to code, and I found no samples online until I saw Melba23's sample here:
    ;   https://www.autoitscript.com/forum/topic/119843-dynamic-gui-problem/#comment-832672
    ; I got this working with only a little modification. THANK YOU MELBA23
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE

            Case $idClose1
                For $i = 0 To $iMMNewCount Step 1
                    Switch GUICtrlRead($aGUICheckbox[$i])
                        Case $GUI_CHECKED
                            $aCheckedVideos[$i] = $aArrayFile[$i]
                        Case $GUI_UNCHECKED
Return $aCheckedVideos

EndFunc   ;==>DisplayVideos


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

  • Similar Content

    • rudi
      By rudi
      for some script I need to investigate the states checked/unchecked, hidden/shown, enabled/disabled to several checkbox controls.
      Any suggestions to this script to retrieve the information in a better way?
      ; Autoit Version ; GuiCtrlCheckState.au3 #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $CtrlW = 100 $CtrlH = 30 $GuiW = 250 $GuiH = 300 GUICreate("Test Checkbox Status", $GuiW, $GuiH) $Ctrl = GUICtrlCreateCheckbox("Checkbox", 10, 10, $CtrlW, $CtrlH) Opt("Guicoordmode", 2) $BtnDisable = GUICtrlCreateButton("Disable", -1, 10) $BtnEnable = GUICtrlCreateButton("Enable", -1, 1) $BtnHide = GUICtrlCreateButton("Hide", 10, -$CtrlH * 2 - 1) $BtnUnHide = GUICtrlCreateButton("Show (Unhide)", -1, 1) $BtnCheckChecked = GUICtrlCreateButton("IsChecked", -$CtrlW * 2 - 10, 40) $BtnCheckEnabled = GUICtrlCreateButton("IsEnabled", -1, 1) $BtnCheckHidden = GUICtrlCreateButton("IsHidden", -1, 1) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $BtnDisable GUICtrlSetState($Ctrl, $gui_disable) Case $BtnEnable GUICtrlSetState($Ctrl, $GUI_Enable) Case $BtnHide GUICtrlSetState($Ctrl, $GUI_Hide) Case $BtnUnHide GUICtrlSetState($Ctrl, $GUI_show) Case $BtnCheckChecked If CheckState($Ctrl, $GUI_CHECKED) Then ConsoleWrite("Checked" & @CRLF) Else ConsoleWrite("not checked" & @CRLF) EndIf Case $BtnCheckEnabled If CheckState($Ctrl, $GUI_Enable) Then ConsoleWrite("enabled" & @CRLF) Else ConsoleWrite("disabled" & @CRLF) EndIf Case $BtnCheckHidden If CheckState($Ctrl, $GUI_Hide) Then ConsoleWrite("Hidden" & @CRLF) Else ConsoleWrite("not hidden" & @CRLF) EndIf EndSwitch WEnd Func CheckState($_CtrlID, $_State = $GUI_CHECKED) ConsoleWrite("---------------------" & @CRLF) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_State to check = ' & $_State & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_Read = GUICtrlRead($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_Read (GuiCtrlRead) = ' & $_Read & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_StateFound = GUICtrlGetState($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_StateFound (GuiCtrlGetState) = ' & $_StateFound & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $_StateFound = -1 Then MsgBox(48, "Control-ID not defined", "Control-ID invalid") Return False ElseIf BitAND($_StateFound, $_State) = $_State Then Return True ElseIf BitAND($_Read, $_State) = $_State Then Return True Else Return False EndIf EndFunc ;==>CheckState Regards, Rudi.
    • DoofGore
      By DoofGore
      Hello All, My first post I know this is pretty noobish but I'm trying to find a way to make this gui working.. searched but as I'm new to autoit I don't understand much... 
      I want to make this gui on top if we press ON TOP button and off on top function when we press on top off.. Created gui with koda ... any help would be appreciated 
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 353, 209, 192, 114) Global $ONTOPON = GUICtrlCreateButton("ON TOP ON", 32, 40, 137, 65) Global $exit = GUICtrlCreateButton("EXIT", 105, 118, 137, 65) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") Global $ONTOPOFF = GUICtrlCreateButton("ON TOP OFF", 184, 41, 137, 65) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $ONTOPON Case $ONTOPOFF Case $exit Exit EndSwitch WEnd  

    • gillesg
      By gillesg
      I am struggling in merging GUITreeViewEx, Shelltristate and enhancing to handle a third state that means : some items under are selected.
      I have difficulties handling expand order and key Space (especially when node is collapsed).
      Here the zip with UDF and and example.
      The problem I might need some advice to handle : 
      1- When load Treeview, have a correct settings of the checkbox for a tristate tree
      2 - Handle keyboard used for walking in tree
           Chicken is checked and  Steak is unchecked
          When walking with arrow to Meat, it gets unchecked
      3 - When node is collapsed and checked thru keyboard (space)
         the middle state is possible which should not
      Here is joined an animated gif showing the 3 problems
      Thanks for your advices

    • computergroove
      By computergroove
      I've made a form in Koda where I make a list of 3 user definable elements that I want to be able to replicate with a button press so a user can add data that can be used as part of the program. 
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form=D:\Carbonite Important\0 - Programing\Scite and Autoit\Koda\Forms\Mileage Estimator.kxf $Form1 = GUICreate("Save Project", 614, 1126, 298, 141) $Location = GUICtrlCreateInput("Location", 40, 112, 345, 21) $Distance = GUICtrlCreateInput("Distance", 400, 112, 89, 21) $AddLocation = GUICtrlCreateButton("AddLocation", 288, 80, 97, 25) $Label1 = GUICtrlCreateLabel("Location", 40, 88, 45, 17) $Label2 = GUICtrlCreateLabel("Distance", 400, 88, 46, 17) $HomeBase = GUICtrlCreateInput("HomeBase", 312, 32, 121, 21) $LeapYear = GUICtrlCreateCheckbox("LeapYear", 464, 32, 17, 17) $Label3 = GUICtrlCreateLabel("Leap Year?", 488, 32, 59, 17) $Variance = GUICtrlCreateInput("Variance", 504, 112, 81, 21) $Label4 = GUICtrlCreateLabel("Variance", 504, 88, 46, 17) $Label5 = GUICtrlCreateLabel("Home Base", 312, 8, 59, 17) $Button1 = GUICtrlCreateButton("Button1", 144, 56, 1, 57) $Save = GUICtrlCreateButton("Save Project", 40, 16, 121, 25) $Load = GUICtrlCreateButton("Load Project", 176, 16, 121, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Form1 Case $AddLocation EndSwitch WEnd I want to press the "add location" button and have the fields {Location, Distance and variance} create a new blank record located under the existing 3 input boxes. Since the fields can number over 30 locations in some examples I am thinking about having these items in a scrolling window. What is the best way to do this and can auto it add a variable while running by pressing a button?
    • XaelloNegative
      By XaelloNegative
      I made a small blunder with my code. I created the GUI under 1920x1080 resolution however, my user is configured to ONLY 1080x720 and cannot go any further than that.
      What do you guys think is the best way to correct this one? Resolution mathematics is something that came to mind however I do not have access to his PC to check  from time to time if  my estimation is correct and the resolution on my PC doesn't go down to 1080x720.
      Please help.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.