Jump to content

How to access an array of checkboxes?


Recommended Posts

Hi,

I have created an array of checkboxes:

CODE

Global $ctl_companySelectionCheckboxes

For $v_i = 1 To $v_allCompanies[0] Step 1 ; $v_allCompanies[0] contains the length of the array.

_ArrayAdd($ctl_companySelectionCheckboxes, GUICtrlCreateCheckbox(GetCompanyName($v_allCompanies[$v_i]), x, y))

EndIf

Next

However when I tried to set their states with "GuiCtrlSetState($ctl_companySelectionCheckboxes[1], $GUI_CHECKED)", I get the following error:

CODE

GuiCtrlSetState($ctl_companySelectionCheckboxes[1], $GUI_CHECKED)

GuiCtrlSetState($ctl_companySelectionCheckboxes^ERROR

Error: Subscript used with non-Array variable.

Am I not accessing those checkoxes in the right way? Please help!

- DB

Link to comment
Share on other sites

_ArrayAdd() does NOT pass the array by reference, therefore you must use the return value to overwrite the array.

$ctl_companySelectionCheckboxes = _ArrayAdd($ctl_companySelectionCheckboxes, GUICtrlCreateCheckbox(GetCompanyName($v_allCompanies[$v_i]), x, y))

Link to comment
Share on other sites

Hi,

Thanks, I have fixed that. How should I access the check boxes in the array? I am still getting the error "Subscript used with non-Array variable".

Am I supposed to create a new checkbox, and set that to something inside the array?

- DB

_ArrayAdd() does NOT pass the array by reference, therefore you must use the return value to overwrite the array.

$ctl_companySelectionCheckboxes = _ArrayAdd($ctl_companySelectionCheckboxes, GUICtrlCreateCheckbox(GetCompanyName($v_allCompanies[$v_i]), x, y))

Link to comment
Share on other sites

You need some error checking:

After _ArrayAdd() -

If @ERROR Then MsgBox(0,"","ArrayAdd Error")

After the loop -

If NOT IsArray($ctl_companySelectionCheckboxes) Then

MsgBox(0,"","Array invalid")

Else

_ArrayDisplay($ctl_companySelectionCheckboxes)

EndIf

Link to comment
Share on other sites

_ArrayAdd() does NOT pass the array by reference...

Yes it does.

Demo:

#include <array.au3>

Global $v_allCompanies[4] = [3, "Red Hat", "Google", "Cannonical"]
Global $ctl_companySelectionCheckboxes[1] = [0]
Global $h = (30 * $v_allCompanies[0]) + 50, $hGUI, $y, $Button

$hGUI = GUICreate("Test", 400, $h)

For $v_i = 1 To $v_allCompanies[0]
    $y = (($v_i - 1) * 30) + 10
    _ArrayAdd($ctl_companySelectionCheckboxes, GUICtrlCreateCheckbox($v_allCompanies[$v_i], 10, $y, 380, 20))
Next
$Button = GUICtrlCreateButton("READ", 150, (30 * $v_allCompanies[0]) + 10, 100, 30)
GUISetState()

Do 
    $msg = GUIGetMsg()
    If $msg = $Button Then
        $sText = "Selections:" & @CRLF
        For $n = 1 To $v_allCompanies[0]
            $sText &= $v_allCompanies[$n] & ":  " & _
                ControlCommand($hGUI, "", $ctl_companySelectionCheckboxes[$n], "IsChecked") & @CRLF
        Next
        MsgBox(64, "Results", $sText)
    EndIf
Until $msg = -3

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Doesn't say that in the help file:

Yes it does.

_ArrayAdd

--------------------------------------------------------------------------------

Adds a specified value at the end of an existing array.

#Include <Array.au3>

_ArrayAdd(ByRef $avArray, $vValue)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Nope, heres the helpfile for 3.2.10.0

Yes it does.

:)

My "GoodMorning" script opens the latest help file I have for me automatically, so I've got 3.2.11.1 open. Looks like the typo was caught already.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Yeah, it Returns 1, and adds the value to the array if it succeeds...

I guess the helpfile should be modified.

In any case, I believe the problem is that the array used when you call _ArrayAdd is not really an array, which fails the _ArrayAdd function:

#include <Array.au3>

Dim $Test

_ArrayAdd($Test, "Test")
If @error Then
    ConsoleWrite("Error: " & @error & @LF)
    Exit
EndIf

For $i = 1 To UBound($Test)-1
    ConsoleWrite($Test[$i] & @LF)
Next

No errors. :)

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