Jump to content

Dynamic Checkboxes in a GUI


Jibberish
 Share

Recommended Posts

Hello,

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.

Cheers!

Jibberish

#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

; MAIN
; 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)
    EndIf
Next

$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
    Next

    $idClose1 = GUICtrlCreateButton("Start", $iL, $iT)
    GUISetState(@SW_SHOW)

    ; 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
                Exit

            Case $idClose1
                For $i = 0 To $iMMNewCount Step 1
                    Switch GUICtrlRead($aGUICheckbox[$i])
                        Case $GUI_CHECKED
                            $aCheckedVideos[$i] = $aArrayFile[$i]
                        Case $GUI_UNCHECKED
                    EndSwitch
                Next
                ExitLoop
        EndSwitch
    WEnd
    GUIDelete($hGUI)
Return $aCheckedVideos

EndFunc   ;==>DisplayVideos

 

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