Jump to content
Sign in to follow this  
ss3vegeta

Dynamically allocated variable names for checkboxes

Recommended Posts

ss3vegeta

Hello,

I'm new to this language and I'm having a little trouble with checkboxes. What Im trying to do is create a list of checkboxes that is created dynamically from a 2 dimensional array to simplify the code:

$loop = 0
$positionTop = $guiHorizontalSpacing*2
    
While $loop < $arraySize
    If $loop <= Ceiling($arraySize/2) Then
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiBorder, $positionTop))
    ElseIf $loop = (Ceiling($arraySize/2)+1) Then
        $positionTop = $guiHorizontalSpacing*2; reset the spacing for second column
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiWidth/2, $positionTop))
    Else
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiWidth/2, $positionTop))
    EndIf
    $loop = $loop + 1
    $positionTop = $positionTop + $guiHorizontalSpacing
WEnd

This seems to work perfectly but Im having a problem trying to simplify the code using GUICtrlRead().

$loop = 0

While $loop < $arraySize
    $activeCheckBox = $programs[$loop][$arrVariableIndex]
    $buttonState = GUICtrlRead($activeCheckBox)
    If $buttonState = $GUI_CHECKED Then
        $programs[$loop][$arrInstallKey] = $installFlag
        $numberOfSoftwareSelected = $numberOfSoftwareSelected + 1
    EndIf
    $loop = $loop + 1
WEnd
If $numberOfSoftwareSelected == 0 Then
    GUIDelete($guiTitle)
Exit
EndIf

Any help that can be offered will be greatly appreciated.

Edited by ss3vegeta

Share this post


Link to post
Share on other sites
wraithdu

From help file under GuiCtrlRead -

For Checkbox, Radio control several states can be returned as $GUI_FOCUS and $GUI_CHECKED,. So use i.e. BitAnd(GUICtrlRead($Item),$GUI_CHECKED) to test if the control is checked.

You could also consider storing just the returned control ID in your array instead of creating a variable for it, then storing the variable.

Edited by wraithdu

Share this post


Link to post
Share on other sites
ss3vegeta

Thanks for the tip. I however can't get it it to work for me either. Is it possible I'm using it incorrectly?

; Main Array declaration
Local $programs[19][4] = [["CheckBox0", "Dot Net All in One Package", "001DNF.exe", 0], ["CheckBox1", "Windows Defender", "MSBGBOX1.exe", 0], ["CheckBox2", "Windows Search", "003SCH.exe", 0], ["CheckBox3", "Calculator Plus", "004Cal.exe", 0], ["CheckBox4", "Office Enterprise 2007 Full Install", "005OE7.exe", 0], ["CheckBox5", "Symantec Endpoint Security", "MSGBOX2.exe", 0], ["CheckBox6", "Notepad++", "007npp.exe", 0], ["CheckBox7", "Acrobat Professional 9", "008AP9.exe", 0], ["CheckBox8", "CDBurnerXP", "009CDB.exe", 0], ["CheckBox9", "Digital Image Suite 2006 With Updates", "010DIS.exe", 0], ["CheckBox10", "FileZilla ", "011FiZ.exe", 0], ["CheckBox11", "FireFox", "012FFX.exe", 0], ["CheckBox12", "Klite Codec Pack", "013KCP.exe", 0], ["CheckBox13", "Microsoft Money 2007", "MSGBOX3.exe", 0], ["CheckBox14", "Firewall", "015pg2.exe", 0], ["CheckBox15", "PowerDVD 8 with Updates", "016PDV.exe", 0], ["CheckBox16", "Contribute Package", "017Sly.exe", 0], ["CheckBox17", "Windows Washer", "018WWa.exe", 0], ["CheckBox18", "Winrar 3.80", "MSGBOX1.exe", 0]]

; Variables
$arrVariableIndex = 0; first element of the second dimension
$arrCheckBoxDesc = 1; second element of the second dimensiobn
$arrFileName = 2; third elementn of the second dimension
$arrInstallKey = 3; fourth element of the second dimension
$arraySize = UBound($programs, 1)
$numberOfSoftwareSelected = 0
$installFlag = 1; this is is used in the third element to flag for install

; Create GUI
$guiBorder = 10; the borders on all sides of the GUI
$guiHorizontalSpacing = 20; The spacing for checkboxes, also used once as vertical spacing between buttons
$guiWidth = 600; The overall width of the GUI
$buttonHeigth = 25; The height of the cancel and install buttons
$buttonWidth = 100; The width of the cancel and install buttons
$guiHeight = (Ceiling($arraySize/2)*$guiHorizontalSpacing) + $buttonHeigth + ($guiBorder*2) + ($guiHorizontalSpacing*3)  
; GuiHeight is the number of software options divided by 2 multiplied by the vertical spacing required for them
; plus the height of the buttons, a border on the top and bottom of the screen, and an additional 2 Horizontal spaces
; to account for spacing between the text area, check boxes and buttons and 1 additional horizontal space for the subtext
$cancelButtonLeftMarker = ($guiWidth - ($buttonWidth*2) - $guiHorizontalSpacing) /2; This positions the cancel button so that both buttons are centered
$installButtonLeftMarker = $guiWidth - $cancelButtonLeftMarker - $buttonWidth; Uses the position of the cancel button to determine the placement of Install button
$guiTitle = "Software Addons"; The title at the top of the GUI
$guiSubText = "Select the progams you would like installed by checking them, then select ""Install""."; Subtext

GUICreate($guiTitle, $guiWidth, $guiHeight)
GUICtrlCreateLabel($guiSubText ,$guiBorder,$guiBorder)
$cancelButton = GUICtrlCreateButton("Cancel", $cancelButtonLeftMarker, $guiHeight - $guiHorizontalSpacing - $guiBorder, $buttonWidth, $buttonHeigth); centers the buttons
$installButton = GUICtrlCreateButton("Install", $installButtonLeftMarker, $guiHeight - $guiHorizontalSpacing - $guiBorder, $buttonWidth, $buttonHeigth); centers the buttons
GUISetState(@SW_SHOW)

$loop = 0
$positionTop = $guiHorizontalSpacing*2

While $loop < $arraySize
    If $loop <= Ceiling($arraySize/2) Then
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiBorder, $positionTop))
    ElseIf $loop = (Ceiling($arraySize/2)+1) Then
        $positionTop = $guiHorizontalSpacing*2; reset the spacing for second column
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiWidth/2, $positionTop))
    Else
        $programs[$loop][$arrVariableIndex] = Assign("CheckBox"&$loop, GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiWidth/2, $positionTop))
    EndIf
    $loop = $loop + 1
    $positionTop = $positionTop + $guiHorizontalSpacing
WEnd

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete($guiTitle); These are done one at a time because case 3 needs to delete before leaving the case
            ExitLoop
        Case $msg = $cancelButton
            GUIDelete($guiTitle)
            ExitLoop
        Case $msg = $installButton
            $loop = 0
        
            While $loop < $arraySize; while the loop is smaller than the array
;$activeCheckBox = $programs[$loop][$arrVariableIndex]; loop through the array and set each one active
;$buttonState = BitAnd(GUICtrlRead($programs[$loop][$arrVariableIndex]),$GUI_CHECKED)
                If BitAnd(GUICtrlRead($programs[$loop][$arrVariableIndex]),$GUI_CHECKED) Then
                    $programs[$loop][$arrInstallKey] = $installFlag
                    $numberOfSoftwareSelected = $numberOfSoftwareSelected + 1
                EndIf
                $loop = $loop + 1
            WEnd
            If $numberOfSoftwareSelected == 0 Then
                GUIDelete($guiTitle)
                Exit
            EndIf
            GUIDelete($guiTitle)
            ProgressOn("Installing", "This could take a while!", "Working")
            $loop = 0
            $ProgressFlag = 0
            
            While $loop < $arraySize
                If $programs[$loop][$arrInstallKey] = $installFlag Then
                    RunWait($programs[$loop][$arrFileName])
                    $ProgressFlag = $ProgressFlag + 1
                    ProgressSet( Ceiling(($ProgressFlag/$numberOfSoftwareSelected) * 100), $ProgressFlag & " of " & $numberOfSoftwareSelected & " programs installed so far.")
                EndIf
                $loop = $loop + 1
            WEnd
            ProgressSet(100, "Installation Complete")
            Sleep(2500)
            ProgressOff()
    EndSelect
WEnd

Thanks,

ss3

Edited by ss3vegeta

Share this post


Link to post
Share on other sites
ss3vegeta

I figured this out, as you suggested I stored the controlID instead of the variable. It works perfectly now.

Thanks for the help,

ss3

Edited by ss3vegeta

Share this post


Link to post
Share on other sites
Damian89

Has anyone fixed this code and gotten it to work?, any chance of getting it? I like the idea of this app.

Thanks

Share this post


Link to post
Share on other sites
ss3vegeta

I don't have this older version of the code anymore. What I can share with you is that I made the array a global variable and it's now populated from a simple text file. The $arrVariableIndex and $arrCheckBoxDesc are just elements of the array... The code I used to dynamically allocate the check boxes is now a function, which I'll happily share. I hope it helps you.

;Display the list of software choices
Func DisplayChoices($arraySize, $guiBorder, $guiHorizontalSpacing, $guiWidth)
$loop = 0
$positionTop = $guiHorizontalSpacing*3

While $loop < $arraySize
    If $loop < Ceiling($arraySize/2) Then
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiBorder*2, $positionTop, ($guiWidth/2)-($guiBorder*2))
    ElseIf $loop = (Ceiling($arraySize/2)) Then
        $positionTop = $guiHorizontalSpacing*3 ; reset the spacing for second column
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], ($guiWidth/2)+$guiBorder, $positionTop, ($guiWidth/2)-($guiBorder*2))
    Else
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], ($guiWidth/2)+$guiBorder, $positionTop, ($guiWidth/2)-($guiBorder*2))
    EndIf
    GUICtrlSetFont($programs[$loop][$arrVariableIndex], 8, 800, 0, "Arial")
    GUICtrlSetColor($programs[$loop][$arrVariableIndex], 0xFFFFFF)
    
    $loop = $loop + 1
    $positionTop = $positionTop + $guiHorizontalSpacing
WEnd
EndFuncoÝ÷ Ù:+y§mê)*'²Ç¥yË^uúèØ^rº1zÂ.±ëaÇîËb¢v®¶­sc²gVæ7FöâvWG2FR6V6¶&÷W2FBfR&VVâ6V6¶VBæB7F÷&W2FV"6öçG&öÂG2£²âFRf'7BVÆVÖVçBöbFR6V6öæBFÖVç6öâöbFR'&¤gVæ2&VD÷Föç56VÆV7FVBb33c¶'&6¦RÂb33c¶ç7FÆÄfÆr b33c¶Æö÷Ò  vÆRb33c¶Æö÷fÇC²b33c¶'&6¦R²vÆRFRÆö÷26ÖÆÆW"FâFR'& b&DæBuT7G&Å&VBb33c·&öw&×5²b33c¶Æö÷Õ²b33c¶'%f&&ÆTæFWÒÂb33c´uTô4T4´TBFVà b33c·&öw&×5²b33c¶Æö÷Õ²b33c¶'$ç7FÆĶWÒÒb33c¶ç7FÆÄfÆp b33c¶çVÖ&W$öe6ögGv&U6VÆV7FVBÒb33c¶çVÖ&W$öe6ögGv&U6VÆV7FVB² VæD` b33c¶Æö÷Òb33c¶Æö÷² tVæ@ bb33c¶çVÖ&W$öe6ögGv&U6VÆV7FVBÓÒFVà uTFVÆWFRb33c¶wVFFÆR W@ VæD`¤VæDgVæ

I hope it helps.

ss3

Edited by ss3vegeta

Share this post


Link to post
Share on other sites
Damian89

Thanks, your a legend this will help me a lot

Share this post


Link to post
Share on other sites
lgwapnitsky

I don't have this older version of the code anymore. What I can share with you is that I made the array a global variable and it's now populated from a simple text file. The $arrVariableIndex and $arrCheckBoxDesc are just elements of the array... The code I used to dynamically allocate the check boxes is now a function, which I'll happily share. I hope it helps you.

;Display the list of software choices
Func DisplayChoices($arraySize, $guiBorder, $guiHorizontalSpacing, $guiWidth)
$loop = 0
$positionTop = $guiHorizontalSpacing*3

While $loop < $arraySize
    If $loop < Ceiling($arraySize/2) Then
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], $guiBorder*2, $positionTop, ($guiWidth/2)-($guiBorder*2))
    ElseIf $loop = (Ceiling($arraySize/2)) Then
        $positionTop = $guiHorizontalSpacing*3 ; reset the spacing for second column
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], ($guiWidth/2)+$guiBorder, $positionTop, ($guiWidth/2)-($guiBorder*2))
    Else
        $programs[$loop][$arrVariableIndex] = GUICtrlCreateCheckbox($programs[$loop][$arrCheckBoxDesc], ($guiWidth/2)+$guiBorder, $positionTop, ($guiWidth/2)-($guiBorder*2))
    EndIf
    GUICtrlSetFont($programs[$loop][$arrVariableIndex], 8, 800, 0, "Arial")
    GUICtrlSetColor($programs[$loop][$arrVariableIndex], 0xFFFFFF)
    
    $loop = $loop + 1
    $positionTop = $positionTop + $guiHorizontalSpacing
WEnd
EndFuncoÝ÷ Ù:+y§mê)*'²Ç¥yË^uúèØ^rº1zÂ.±ëaÇîËb¢v®¶­sc²gVæ7FöâvWG2FR6V6¶&÷W2FBfR&VVâ6V6¶VBæB7F÷&W2FV"6öçG&öÂG2£²âFRf'7BVÆVÖVçBöbFR6V6öæBFÖVç6öâöbFR'&¤gVæ2&VD÷Föç56VÆV7FVBb33c¶'&6¦RÂb33c¶ç7FÆÄfÆr b33c¶Æö÷Ò  vÆRb33c¶Æö÷fÇC²b33c¶'&6¦R²vÆRFRÆö÷26ÖÆÆW"FâFR'& b&DæBuT7G&Å&VBb33c·&öw&×5²b33c¶Æö÷Õ²b33c¶'%f&&ÆTæFWÒÂb33c´uTô4T4´TBFVà b33c·&öw&×5²b33c¶Æö÷Õ²b33c¶'$ç7FÆĶWÒÒb33c¶ç7FÆÄfÆp b33c¶çVÖ&W$öe6ögGv&U6VÆV7FVBÒb33c¶çVÖ&W$öe6ögGv&U6VÆV7FVB² VæD` b33c¶Æö÷Òb33c¶Æö÷² tVæ@ bb33c¶çVÖ&W$öe6ögGv&U6VÆV7FVBÓÒFVà uTFVÆWFRb33c¶wVFFÆR W@ VæD`¤VæDgVæ

I hope it helps.

ss3

The code seems to have gotten a little "quirky" at the end. Can you repost this?

Thank you,

Larry

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  

×