Jump to content

Creating Multiple Check boxes based on a loop


ttleser
 Share

Recommended Posts

I've searched the forums and found several articles but I guess I'm missing the idea of being able to create multiple checkboxes in a loop. Ok, let me revise that. I can create them fine, but I'm struggling to understand how to interact with the checkboxes.

I want to create a GUI to read one INI file, creating checkboxes based on the sections in that INI file. This I'm able to do. However, I can't figure out how to read or set the state of the checkbox.

Here's a sample script so far. Keep in mind I've been playing with different things to see what results are to try other things to see if I can change the state of a checkbox or read the state, but nothing is helping. The script isn't the cleanest thing in the world right now due to the troubleshooting I'm doing.

Here's some of the code (Not All):

#include <GUIConstantsEx.au3>
#Include <File.au3>
#Include <Array.au3>
#include <String.au3>
#include <EditConstants.au3>
#include <Constants.au3>
Dim $CheckBoxes[2], $OutputArray[2][2], $objWMIService, $CheckBoxArray[2][2]
$b = 2
$Sections = IniReadSection(@ScriptDir&"\GWPInfo.ini","Options")
;_ArrayDisplay($Sections)
GUICreate("GWPInfo",300,300)
GuiCtrlCreateLabel("The helpdesk will advise which options to choose.",10,20,280,20)
;$Basic = GUICtrlCreateCheckbox("Basic",10,40,100,20)
For $a = 1 to $Sections[0][0]
If StringInStr(($a/2),".") > 0 Then
;ConsoleWrite($Sections[$a][0]&@CRLF)
ReDim $CheckBoxes[$a+1]
ReDim $CheckBoxArray[$a+1][2]
$CheckBoxes[$a] = GUICtrlCreateCheckbox($Sections[$a][0],10, $b * 20,100,20)
$CheckBoxes[0] = $a
$CheckBoxArray[0][0] = $a
$CheckBoxArray[$a][0] = $Sections[$a][0]
;ConsoleWrite("1"&@CRLF)
$CheckBoxArray[$a][1] = $CheckBoxes[$a]
;ConsoleWrite("2"&@CRLF)
$b = $b + 1
ConsoleWrite("$CheckBoxes[$a] for "&$Sections[$a][0]&" = "&$CheckBoxes[$a]&@CRLF)
Else
;ConsoleWrite($Sections[$a][0]&@CRLF)
ReDim $CheckBoxes[$a+1]
ReDim $CheckBoxArray[$a+1][2]
$CheckBoxes[$a] = GUICtrlCreateCheckbox($Sections[$a][0],160, $b * 20,100,20)
$CheckBoxes[0] = $a
$CheckBoxArray[0][0] = $a
$CheckBoxArray[$a][0] = $Sections[$a][0]
;ConsoleWrite("3"&@CRLF)
$CheckBoxArray[$a][1] = $CheckBoxes[$a]
ConsoleWrite("$CheckBoxes[$a] for "&$Sections[$a][0]&" = "&$CheckBoxes[$a]&@CRLF)
;ConsoleWrite("4"&@CRLF)
EndIf
Next
_ArrayDisplay($CheckBoxes,"$Checkboxes")
;_ArrayDisplay($CheckBoxArray,"$CheckboxArray")
For $f = $CheckboxArray[1][0] to $CheckboxArray[0][0] + $CheckboxArray[1][0]
If $CheckBoxArray[$f][0] = "Basic" Then
ConsoleWrite("Found Basic"&@CRLF)
ConsoleWrite("Basic's GUI ID = "&$CheckBoxArray[$f][1]&@CRLF)
;GuiCtrlSetState($CheckBoxes[$CheckBoxArray[$f][1]],$GUI_CHECKED)
GuiCtrlSetState($CheckBoxes[4],$GUI_CHECKED)
GuiCtrlSetState($CheckBoxes[$CheckBoxArray[$f][1]],$GUI_DISABLE)
ExitLoop
EndIf
Next

$Start = GUICtrlCreateButton("START",110,230,80,40,0x0001)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $Start
;_ArrayDisplay($CheckBoxes,"$CheckBoxes")
GatherBasic()
For $c = 2 to $Sections[0]
If $CheckBoxes[$c] And BitAND(GUICtrlRead($CheckBoxes[$c]), $GUI_CHECKED) = $GUI_CHECKED Then
EndIf
Next
ExitLoop
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
EndSelect
WEnd

Func GatherBasic()
;Do something
EndFunc

Here's the INI:

[Options]
Basic=Basic.ini
Network=Network.ini
SAP=SAP.ini

When you run it, it'll display the array of $checkboxes that'll show:

[0] > 3

[1] > 4

[2] > 5

[3] > 6

Closing out the array will show:

$CheckBoxes[$a] for Basic = 4

$CheckBoxes[$a] for Network = 5

$CheckBoxes[$a] for SAP = 6

Found Basic

Basic's GUI ID = 4

L:\Utilities\GWPInfo\GWPInfo.au3 (52) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

GuiCtrlSetState($CheckBoxes[4],$GUI_CHECKED)

GuiCtrlSetState(^ ERROR

Now, again keep in mind that the original line it's error'ing out on looked like GuiCtrlSetState($CheckBoxes[$CheckBoxArray[$f][1]],$GUI_CHECKED). I changed it to GuiCtrlSetState($CheckBoxes[4],$GUI_CHECKED) just to see if my syntax was wrong (which it may still be).

Did I setup the Checkboxes wrong?

Link to comment
Share on other sites

I'm trying to follow what you're doing in this script and I'm lost as to what that might be.

$Checkboxes[4] is an error because the array only has 4 elements in it (0-3).

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You're using the control ID ($CheckBoxArray[$f][1]) as the index into the $CheckBoxes array. This is wrong obviously. You need to search the [$f][1] dimension of the $CheckBoxArray array for the matching control ID, then use that index to access the $CheckBoxes array.

That said... why would you do that anyway? $CheckBoxArray[$a][1] has the same value that you stored in $CheckBoxes[$a] (you copied it in line 24). That ID value is what you need for GUICtrlSetState anyway, so why do you need to keep it in two places?

Edited by wraithdu
Link to comment
Share on other sites

I thought "4" (since it showed up as a value in the $CheckBoxes array) would be the handle for that control. If the "4" isn't a handle, then what exactly is it?

So I guess I went about it wrong, hence why I'm asking for help. ;)

So let's start at the beginning. How do I dynamically create checkboxes based on sectionnames in an INI file and be able to retrieve the states using a loop to create them?

Edited by ttleser
Link to comment
Share on other sites

  • Moderators

ttleser,

did I offend you guys?

Not as far as I know. :)

This might give you a clue: ;)

#include <GUIConstantsEx.au3>

; Declare variables
$sIni = "GWPInfo.ini"

$hGUI = GUICreate("Test", 500, 500)

$aData = IniReadSection($sIni, "Options")

$iCount = $aData[0][0]

; Create an array to hold the ControlIDs of the checkboxes
Global $aCheck[$iCount + 1]

For $i = 1 To $iCount
    ; Store controIDs of the checkboxes
    $aCheck[$i] = GUICtrlCreateCheckbox($aData[$i][0], 10, $i * 20, 100, 20)
Next

GUISetState()

While 1

    ; Wait for an event
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aCheck[1] To $aCheck[$iCount]
            ; Which box was it?
            For $i = 1 To $iCount
                If $iMsg = $aCheck[$i] Then
                    ; Is it checked or not?
                    $sState = "Checked"
                    If GUICtrlRead($iMsg) <> 1 Then
                        $sState = "Unchecked"
                    EndIf
                    ; Announce the result
                    MsgBox(0, "Altered", $aData[$i][0] & @CRLF & "Current state: " & $sState)
                EndIf
            Next

    EndSwitch

WEnd

The "4" you mention is the ControlID of the control - an index to an internal array within AutoIt to identify the control. ;)

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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