BSoft

CheckBox Array - Common handler Question

6 posts in this topic

#1 ·  Posted (edited)

Hi, I have an array of Check Box controls and want to provide a common handler function that can be called with the CheckBox Index as the parameter. Is there a way to detect when any CheckBox in an array of CheckBoxes is clicked and call a common handler function and pass the index of the CheckBox that was clicked to the common function?

At the moment I am doing this manually and it seems a little messy - especially if I were to increase the number of Check Boxes in the array. (See sample code below)

; Create CheckBoxes for each Market in an Array
$i = 0
$iSpacing = 24
While $i < $g_iMarketCount
    $temp = $g_MarketIdProc[$i]
    $g_CtrlMarketCheckBox[$i] = GUICtrlCreateCheckbox($temp, 80, $iSpacing, 65, 17, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
    $i = $i + 1
    $iSpacing = $iSpacing + 20
WEnd

Then in my loop I have

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        


        Case $g_CtrlMarketCheckBox[0]
            MyUpdateMarket(0)

        Case $g_CtrlMarketCheckBox[1]
            MyUpdateMarket(1)

        Case $g_CtrlMarketCheckBox[2]
            MyUpdateMarket(2)

        Case $g_CtrlMarketCheckBox[3]
            MyUpdateMarket(3)

        Case $g_CtrlMarketCheckBox[4]
            MyUpdateMarket(4)

        Case $g_CtrlMarketCheckBox[5]
            MyUpdateMarket(5)

        Case $g_CtrlMarketCheckBox[6]
            MyUpdateMarket(6)

        Case $g_CtrlMarketCheckBox[7]
            MyUpdateMarket(7)

        Case $g_CtrlMarketCheckBox[8]
            MyUpdateMarket(8)

        Case $g_CtrlMarketCheckBox[9]
            MyUpdateMarket(9)

        Case $g_CtrlMarketCheckBox[10]
            MyUpdateMarket(10)

        Case $g_CtrlMarketCheckBox[11]
            MyUpdateMarket(11)

        Case $g_CtrlMarketCheckBox[12]
            MyUpdateMarket(12)

        Case $g_CtrlMarketCheckBox[13]
            MyUpdateMarket(13)

        Case $g_CtrlMarketCheckBox[14]
            MyUpdateMarket(14)

    EndSwitch
WEnd

It would be nice to be able to replace this with just a single common function call.

 

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites



BSoft,

A simple loop through the array checking the stored ControlID against $nMsg would seem to meet your requirements:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>

Global $g_iMarketCount = 10
Global $g_CtrlMarketCheckBox[$g_iMarketCount]

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

; Create CheckBoxes for each Market in an Array
$iSpacing = 24
For $i = 0 To $g_iMarketCount - 1
    $g_CtrlMarketCheckBox[$i] = GUICtrlCreateCheckbox($i, 80, $iSpacing + (20 * $i), 65, 17, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
Next

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 0 To $g_iMarketCount - 1
                If $g_CtrlMarketCheckBox[$i] = $nMsg Then
                    MsgBox($MB_SYSTEMMODAL, "Pressed", $i)
                    ; No point in looking further
                    ExitLoop

                EndIf

            Next
    EndSwitch

WEnd

I simplified the creation code a bit as well.

M23

P.S. When you post code please use Code tags - see here how to do it.  Then you get a scrolling box and syntax colouring as you can see above now I have added the tags.

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Thank you again Melba23. Simple and very clean!

Thank you also for the tip re posting - will do in future now.

Share this post


Link to post
Share on other sites

The "Case Else" can be optimized:

Case $g_CtrlMarketCheckBox[0] To $g_CtrlMarketCheckBox[$g_iMarketCount-1]
    MsgBox($MB_SYSTEMMODAL, "Pressed", $nMsg - $g_CtrlMarketCheckBox[0])

 

Share this post


Link to post
Share on other sites

LarsJ,

True - if the checkboxes are created in immediate succession, which normally results in an array of consecutive ControlIDs. But this is undocumented and not guaranteed (especially if other controls have been deleted previously) so I would always recommend using the method I posted.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

For reference, you can also use OnEvent instead of a GetMSG loop and utilize @CtrlID as an alternative.
Here's a link to an example which demonstrates what I mean.

Clicking a Checkbox calls the _Clicked function and the function interacts with @CtrlID

Depending on what your are trying to do with it of course, this may allow you to interact with a specific Checkbox control without having to track them in an Array--although I've certainly used an array to track controls in my scripts before :P

Just wanted to give another ideas
 

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