Jump to content
BSoft

CheckBox Array - Common handler Question - (Locked)

Recommended Posts

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.

 


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

 

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


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

 

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
Guest

Hi M23

I have started learning autoit scripts last week and your tips have been very helpful. I am trying to

  • Read checkboxes from an existing array variable, and
  • Modify the array based on the state of the checkboxes changing as they are being ticked.

My array has my labels in the 3rd column, and the checkboxes start on column 5 (I have not included the array as it is so big.

I have 2 issues:

  • As I was trying to make it fit on screen, I had to either use scroll bars or use tabs; your one command scrollbar worked great, except the scrolling froze due to the size of the array; when I tried to split into tabs, all of the checkboxes and labels appear on the first tab and then every other tab is empty
  • As I open the GUI, the positions marked as 1 in the array do not appear as checked (I am probably doing this wrong in my loop); because of this my script probably also isn't saving anything into the array with my loop

You wrote in another threat that listview doesn't play nice with tabs, so I may be falling into the same kind of issue with array check boxes. For me it is important to have everything in an array because the check boxes will determine information to look for in another function of the script.

I tried adding the tab closing statement in the for..to function for the last value of $i and $j and that also didn't work.

Regarding having my tickmarks pre-checked based on the initials state of the array, I must admit I haven't yet started to explore my options. I would be really grateful if you could point me in the right direction.

;~               $array_mods_[x][0] = Category
;~               $array_mods_[x][1] = Name
;~               $array_mods_[x][2] = Description
;~               $array_mods_[x][3] = Modstring
;~               $array_mods_[x][4-14] = checkmarks for each of the labels below

$array_weaponmods[126][15]
$ColLabels[11] = ["Axe", "Bow", "Offhand", "Hammer", "Wand", "Shield", "Staff", "Sword" , "Daggers", "Scythe", "Spear"]
$g_iMarketCount = 50
$g_CtrlMarketCheckBox[$g_iMarketCount]
$iSpacing = 8

Global $frmSelection = GUICreate("Mod Selection", 808, 624, 500, 112)

GUICtrlCreateTab(8, 8, 792, 608)

GUICtrlCreateTabItem("Popular Mods")
   For $i = 0 To 30
       GUICtrlCreateLabel($array_weaponmods[$i+1][2], $X_GUI+$iSpacing, 60 + (20 * $i),200, $iSpacing*2)
       For $j = 4 to 14
         If $i=0 then GUICtrlCreateLabel($ColLabels[$j-4], $X_GUI + 20+(50 * $j), 40 + (20 * $i), 40, $iSpacing*2, $ES_CENTER)
         $array_weaponmods[$i][$j] = GUICtrlCreateCheckbox("", $X_GUI + 20+(50 * $j), 60 + (20 * $i), 40, $iSpacing*2, $ES_CENTER)
   ;      GUICtrlCreateCheckbox($array_weaponmods[$i][4], $iSpacing + 200 + (20 * $j), $iSpacing + (20 * $i), 96, $iSpacing * 2, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
   ;      GUICtrlCreateCheckbox($array_weaponmods[$i][2], $X_GUI+$iSpacing, $iSpacing + (20 * $i), 200, $iSpacing*2, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
       Next
    Next

GUICtrlCreateTabItem("") ; end tabitem definition

GUICtrlCreateTabItem("Armor")
   For $i = 31 To 124
       GUICtrlCreateLabel($array_weaponmods[$i+1][2], $X_GUI+$iSpacing, $iSpacing + (20 * ($i+1)),200, $iSpacing*2)
       For $j = 4 to 14
         If $i=0 then GUICtrlCreateLabel($ColLabels[$j-4], $X_GUI + 20+(50 * $j), $Y_GUI + (20 * $i), 40, $iSpacing*2, $ES_CENTER)
         $array_weaponmods[$i][$j] = GUICtrlCreateCheckbox(" ", $X_GUI + 20+(50 * $j), $Y_GUI + 20 + (20 * $i), 40, $iSpacing*2, $ES_CENTER)
   ;      GUICtrlCreateCheckbox($array_weaponmods[$i][4], $iSpacing + 200 + (20 * $j), $iSpacing + (20 * $i), 96, $iSpacing * 2, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
   ;      GUICtrlCreateCheckbox($array_weaponmods[$i][2], $X_GUI+$iSpacing, $iSpacing + (20 * $i), 200, $iSpacing*2, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_PUSHLIKE))
       Next
    Next

GUICtrlCreateTabItem("") ; end tabitem definition

GUISetOnEvent($GUI_EVENT_CLOSE, "ExitBot")


GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 0 To 124
                For $j = 4 to 14
                If $array_weaponmods[$i][$j] = 1 then
                    GUIctrlsetstate ($array_weaponmods[$i][$j], $GUI_CHECKED)
                Endif
                If $array_weaponmods[$i][$j] = $nMsg Then
                    GUIctrlsetstate ($array_weaponmods[$i][$j], $GUI_CHECKED)
                    ; No point in looking further
                    ExitLoop
                EndIf
                Next
            Next
    EndSwitch

WEnd

I have attached a picture of the GUI, as you can see the rows beyond 24 should go onto the next tab but as it is now, they just overflow in the first tab. :-(

GUI.png

Share this post


Link to post
Share on other sites

I would strongly suggest that you read forum rules, especially the part about game automation.  As you will see, it is not allowed to discuss such matter in this forum.

Share this post


Link to post
Share on other sites

Welcome to the AutoIt forum.

Unfortunately you appear to have missed the Forum rules on your way in. Please read them now - particularly the bit about not discussing game automation - and then you will understand why you will get no help and this thread will now be locked.

See you soon with a legitimate question I hope.

The Moderation team


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...