Sign in to follow this  
Followers 0
pl123

GUICtrlSetOnEvent

4 posts in this topic

I'm trying to create two buttons, one of which will check all the checkboxes, and the other will uncheck them, but it isn't working. When I click the buttons, nothing happens.

GUICreate ("title", 400, 450, 325, 250)
GUISetState(@SW_SHOW)
...
GUICtrlCreateCheckbox("...", 10, 140, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 160, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 180, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 200, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 220, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 240, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 260, "", "", $BS_AUTOCHECKBOX)
GUICtrlCreateCheckbox("...", 10, 280, "", "", $BS_AUTOCHECKBOX)
...

The CtrlIDs are 10 for the first one, then 11 for the next, and so on. The last one's 17.

So right now, everything's working fine, everything's in the right place.

Now, the buttons...

GUICtrlCreateButton("Check all", 315, 140, 70, 20, $BS_VCENTER + $BS_CENTER) ;ctrlID = 19
GUICtrlCreateButton("Check none", 315, 160, 70, 20, $BS_VCENTER + $BS_CENTER) ;ctrlID = 20

And here is where I don't understand why it doesn't work. I'm still a beginner with GUIs, so I don't know what's wrong.

GUICtrlSetOnEvent (19, "_CheckAll")
...
Func _CheckAll()
    GUICtrlSetState (10, $GUI_CHECKED)
    GUICtrlSetState (11, $GUI_CHECKED)
    GUICtrlSetState (12, $GUI_CHECKED)
    GUICtrlSetState (13, $GUI_CHECKED)
    GUICtrlSetState (14, $GUI_CHECKED)
    GUICtrlSetState (15, $GUI_CHECKED)
    GUICtrlSetState (16, $GUI_CHECKED)
    GUICtrlSetState (17, $GUI_CHECKED)
EndFunc

Either I did something wrong with the GUICtrlSetOnEvent, the Func _CheckAll(), or something else. Can someone help me with this problem?

Share this post


Link to post
Share on other sites



pl123,

First piece of advice - do NOT use raw ControlIds. If ever you need to insert another control in your script, nothing will work!

Use variables to store the returned ControlIDs. If you do not want to use a variable for each control (and if not, how are you going to detect when the control is used?) you can use Dummy controls like this:

#include <ButtonConstants.au3>

Opt("GUIOnEventMode", 1)

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

GUICreate ("TEST", 400, 450, 325, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState(@SW_SHOW)

$iStart = GUICtrlCreateDummy()
GUICtrlCreateCheckbox("...", 10, 140)
GUICtrlCreateCheckbox("...", 10, 160)
GUICtrlCreateCheckbox("...", 10, 180)
GUICtrlCreateCheckbox("...", 10, 200)
GUICtrlCreateCheckbox("...", 10, 220)
GUICtrlCreateCheckbox("...", 10, 240)
GUICtrlCreateCheckbox("...", 10, 260)
GUICtrlCreateCheckbox("...", 10, 280)
$iEnd = GUICtrlCreateDummy()

$hButton_Check = GUICtrlCreateButton("Check all", 315, 140, 70, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($hButton_Check, "_CheckAll")
$hButton_UnCheck = GUICtrlCreateButton("Check none", 315, 160, 70, 20, $BS_VCENTER + $BS_CENTER) ;ctrlID = 20
GUICtrlSetOnEvent($hButton_UnCheck, "_UnCheckAll")

GUISetState()

While 1
    Sleep(10)
WEnd

Func _CheckAll()
    For $i = $iStart To $iEnd
        GUICtrlSetState($i, $GUI_CHECKED)
    Next
EndFunc

Func _UnCheckAll()
    For $i = $iStart To $iEnd
        GUICtrlSetState($i, $GUI_UNCHECKED)
    Next
EndFunc

Func _Exit()
    Exit
EndFunc

Much better would be code like this: :mellow:

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

Opt("GUIOnEventMode", 1)

Global $aCheckboxID[8]

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

GUICreate ("TEST", 400, 450, 325, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState(@SW_SHOW)

$aCheckboxID[0] = GUICtrlCreateCheckbox("...", 10, 140)
$aCheckboxID[1] = GUICtrlCreateCheckbox("...", 10, 160)
$aCheckboxID[2] = GUICtrlCreateCheckbox("...", 10, 180)
$aCheckboxID[3] = GUICtrlCreateCheckbox("...", 10, 200)
$aCheckboxID[4] = GUICtrlCreateCheckbox("...", 10, 220)
$aCheckboxID[5] = GUICtrlCreateCheckbox("...", 10, 240)
$aCheckboxID[6] = GUICtrlCreateCheckbox("...", 10, 260)
$aCheckboxID[7] = GUICtrlCreateCheckbox("...", 10, 280)

$hButton_Check = GUICtrlCreateButton("Check all", 315, 140, 70, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($hButton_Check, "_CheckAll")
$hButton_UnCheck = GUICtrlCreateButton("Check none", 315, 160, 70, 20, $BS_VCENTER + $BS_CENTER) ;ctrlID = 20
GUICtrlSetOnEvent($hButton_UnCheck, "_UnCheckAll")

GUISetState()

While 1
    Sleep(10)
WEnd

Func _CheckAll()
    For $i = 0 To 7
        GUICtrlSetState($aCheckboxID[$i], $GUI_CHECKED)
    Next
EndFunc

Func _UnCheckAll()
    For $i = 0 To 7
        GUICtrlSetState($aCheckboxID[$i], $GUI_UNCHECKED)
    Next
EndFunc

Func _Exit()
    Exit
EndFunc

where you have IDs for all the checkboxes and can now determine, for example, whether an individual one is checked or not.

I also removed the unnecessary $BS_AUTOCHECKBOX style from the checkboxes - according to the help file it is a default forced style. :party:

Does this make sense? Please ask if not. :P

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

@Melba23

I understand what you mean about the control IDs, and your suggestion works fine for me. Thanks for your help!

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  
Followers 0