Jump to content
Sign in to follow this  
pl123

Check All and Check None funcs

Recommended Posts

pl123

I made a GUI, and it has lots of checkboxes, so I made two buttons, one of which would uncheck all the boxes, one of which would check all the boxes. It didn't work, but with the advice of some people, I got them working. Now, after I added tons more of stuff, they don't seem to work anymore.

Here's a largely abbreviated version of my script...

; -----------------------------------------------
; --------------- Include files -----------------
; -----------------------------------------------

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Misc.au3>
#include <Process.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>

; -----------------------------------------------
; ----------------- Functions -------------------
; -----------------------------------------------

; _Continue() - Close "About" box
; _CheckAll() - Check all boxes
; _UncheckAll() - Uncheck all boxes
; _Settings() - Create settings menu
; _Exit() - Close script
; _DoNothing() - Don't do anything
; _DrawLine() - Draw the divider line for the GUI

; -----------------------------------------------
; ------------------ Variables ------------------
; -----------------------------------------------

Global $Checkbox[8], $InputID[2]
Global $Settings, $Calculate, $GUI
Global $Title = "Test"
Global $Pen, $Graphic

; -----------------------------------------------
; --------------- Read .ini file ----------------
; -----------------------------------------------

; space filler for .ini file

If FileExists(@ScriptDir & "\settings.ini") Then
    $open = IniRead(@ScriptDir & "\settings.ini", "settings", "open", "")
Else
    IniWrite(@ScriptDir & "\settings.ini", "settings", "open", "1")
EndIf

; -----------------------------------------------
; ------------- Create main menu ----------------
; -----------------------------------------------

If _Singleton($GUI) = 0 Then
    MsgBox (48, "Error", "...")
EndIf

$GUI = GUICreate($Title, 400, 450, 350, 250, $WS_OVERLAPPEDWINDOW)
WinSetOnTop($Title, "", 1)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("...", 10, 10)
_DrawLine()
GUICtrlCreateLabel("...", 10, 45)
$InputID[0] = GUICtrlCreateInput("", 10, 65, 100, 20, $ES_NUMBER)
$InputID[1] = GUICtrlCreateInput("", 225, 90, 85, "", $ES_READONLY)
GUICtrlCreateLabel("...", 10, 90)
GUICtrlCreateLabel("...", 10, 115)
$Calculate = GUICtrlCreateButton("...", 325, 90, 60, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($Calculate, "_Calculate")
$Settings = GUICtrlCreateButton("...", 325, 110, 60, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($Settings, "_Settings")
GUICtrlCreateLabel("...", 10, 155)
$Checkbox[0] = GUICtrlCreateCheckbox("...", 10, 175)
$Checkbox[1] = GUICtrlCreateCheckbox("...", 10, 195)
$Checkbox[2] = GUICtrlCreateCheckbox("...", 10, 215)
$Checkbox[3] = GUICtrlCreateCheckbox("...", 10, 235)
$Checkbox[4] = GUICtrlCreateCheckbox("...", 10, 255)
$Checkbox[5] = GUICtrlCreateCheckbox("...", 10, 275)
$Checkbox[6] = GUICtrlCreateCheckbox("...", 10, 295)
$Checkbox[7] = GUICtrlCreateCheckbox("...", 10, 315)
$CheckAll = GUICtrlCreateButton("Check all", 315, 175, 70, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($CheckAll, "_CheckAll")
$UncheckAll = GUICtrlCreateButton("Check none", 315, 195, 70, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlSetOnEvent($UncheckAll, "_UncheckAll")
GUICtrlCreateLabel("...", 10, 370)
While 1
    $nMsg = GUIGetMsg(0)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

; -----------------------------------------------
; --------------- main functions! ---------------
; -----------------------------------------------

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

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

Func _Settings()
    GUICreate("Settings", 350, 350, 325, 250, $WS_OVERLAPPEDWINDOW)
    WinSetOnTop("Settings", "", 1)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg(0)
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>_Settings

Func _DrawLine()
    _GDIPlus_Startup ()
    $Graphic = _GDIPlus_GraphicsCreateFromHWND ($GUI)
    $Pen = _GDIPlus_PenCreate ("", 20, 2)
    _GDIPlus_GraphicsDrawLine ($Graphic, 10, 40, 375, 40)
    _GDIPlus_PenDispose ($Pen)
    _GDIPlus_GraphicsDispose ($Graphic)
    _GDIPlus_ShutDown ()
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _DoNothing()
EndFunc   ;==>_DoNothing

Also, I need help with something else: when I click one of the buttons, I want a new GUI to popup, so I use GUICtrlSetOnEvent, but it doesn't work. Can someone tell me how to do this so it works?

Share this post


Link to post
Share on other sites
Spiff59

You seem to have misplaced your "Opt("GUIOnEventMode", 1)" statement.

But I don't think your $nmsg loop with the $GUI_EVENT_CLOSE test will ever work.

You either go the OnEventMode route, or, the GUIGetMsg() route, like:

; -----------------------------------------------
; --------------- Include files -----------------
; -----------------------------------------------

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Misc.au3>
#include <Process.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
; -----------------------------------------------
; ----------------- Functions -------------------
; -----------------------------------------------

; _Continue() - Close "About" box
; _CheckAll() - Check all boxes
; _UncheckAll() - Uncheck all boxes
; _Settings() - Create settings menu
; _Exit() - Close script
; _DoNothing() - Don't do anything
; _DrawLine() - Draw the divider line for the GUI

; -----------------------------------------------
; ------------------ Variables ------------------
; -----------------------------------------------

Global $Checkbox[8], $InputID[2]
Global $Settings, $Calculate, $GUI
Global $Title = "Test"
Global $Pen, $Graphic

; -----------------------------------------------
; --------------- Read .ini file ----------------
; -----------------------------------------------

; space filler for .ini file

If FileExists(@ScriptDir & "\settings.ini") Then
    $open = IniRead(@ScriptDir & "\settings.ini", "settings", "open", "")
Else
    IniWrite(@ScriptDir & "\settings.ini", "settings", "open", "1")
EndIf

; -----------------------------------------------
; ------------- Create main menu ----------------
; -----------------------------------------------

If _Singleton($GUI) = 0 Then
    MsgBox (48, "Error", "...")
EndIf

;Opt("GUIOnEventMode", 1)

$GUI = GUICreate($Title, 400, 450, 350, 250, $WS_OVERLAPPEDWINDOW)
WinSetOnTop($Title, "", 1)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("...", 10, 10)
;_DrawLine()
GUICtrlCreateLabel("...", 10, 45)
$InputID[0] = GUICtrlCreateInput("", 10, 65, 100, 20, $ES_NUMBER)
$InputID[1] = GUICtrlCreateInput("", 225, 90, 85, "", $ES_READONLY)
GUICtrlCreateLabel("...", 10, 90)
GUICtrlCreateLabel("...", 10, 115)
$Calculate = GUICtrlCreateButton("...", 325, 90, 60, 20, $BS_VCENTER + $BS_CENTER)
$Settings = GUICtrlCreateButton("...", 325, 110, 60, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlCreateLabel("...", 10, 155)
For $i = 0 to 7
    $Checkbox[$i] = GUICtrlCreateCheckbox("...", 10, 175 + $i *20)
Next
$CheckAll = GUICtrlCreateButton("Check all", 315, 175, 70, 20, $BS_VCENTER + $BS_CENTER)
$UncheckAll = GUICtrlCreateButton("Check none", 315, 195, 70, 20, $BS_VCENTER + $BS_CENTER)
GUICtrlCreateLabel("...", 10, 370)
While 1
    $nMsg = GUIGetMsg(0)
    Switch $nMsg
        Case $CheckAll
            _CheckAll()
        Case $UncheckAll
            _UncheckAll()
        Case $Checkbox[0] to $Checkbox[7]
            _Calculate($nmsg - $Checkbox[0])
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

; -----------------------------------------------
; --------------- main functions! ---------------
; -----------------------------------------------

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

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

Func _Settings()
    GUICreate("Settings", 350, 350, 325, 250, $WS_OVERLAPPEDWINDOW)
    WinSetOnTop("Settings", "", 1)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg(0)
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>_Settings

Func _DrawLine()
    _GDIPlus_Startup ()
    $Graphic = _GDIPlus_GraphicsCreateFromHWND ($GUI)
    $Pen = _GDIPlus_PenCreate ("", 20, 2)
    _GDIPlus_GraphicsDrawLine ($Graphic, 10, 40, 375, 40)
    _GDIPlus_PenDispose ($Pen)
    _GDIPlus_GraphicsDispose ($Graphic)
    _GDIPlus_ShutDown ()
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _DoNothing()
EndFunc   ;==>_DoNothing

Func _Calculate($x)
    If GUICtrlRead($Checkbox[$x]) = $GUI_CHECKED Then
        ToolTip("Checkbox " & $x & " was checked")
    Else
        ToolTip("Checkbox " & $x & " was unchecked")
    EndIf
EndFunc

edit: souped-up the tooltip

Edited by Spiff59

Share this post


Link to post
Share on other sites
Melba23

pl123,

You cannot mix OnEvent and GetMessage mode - you need to use one or the other. :P

Here is your script cpnverted to GetMessage mode:

; -----------------------------------------------
; --------------- Include files -----------------
; -----------------------------------------------

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Misc.au3>
#include <Process.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>

; -----------------------------------------------
; ----------------- Functions -------------------
; -----------------------------------------------

; _Continue() - Close "About" box
; _CheckAll() - Check all boxes
; _UncheckAll() - Uncheck all boxes
; _Settings() - Create settings menu
; _Exit() - Close script
; _DoNothing() - Don't do anything
; _DrawLine() - Draw the divider line for the GUI

; -----------------------------------------------
; ------------------ Variables ------------------
; -----------------------------------------------

Global $Checkbox[8], $InputID[2]
Global $Settings, $Calculate, $GUI
Global $Title = "Test"
Global $Pen, $Graphic

; -----------------------------------------------
; --------------- Read .ini file ----------------
; -----------------------------------------------

; space filler for .ini file

If FileExists(@ScriptDir & "\settings.ini") Then
    $open = IniRead(@ScriptDir & "\settings.ini", "settings", "open", "")
Else
    IniWrite(@ScriptDir & "\settings.ini", "settings", "open", "1")
EndIf

; -----------------------------------------------
; ------------- Create main menu ----------------
; -----------------------------------------------

If _Singleton($GUI) = 0 Then
    MsgBox (48, "Error", "...")
EndIf

$GUI = GUICreate($Title, 400, 450, 350, 250, $WS_OVERLAPPEDWINDOW)
WinSetOnTop($Title, "", 1)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("...", 10, 10)
_DrawLine()
GUICtrlCreateLabel("...", 10, 45)
$InputID[0] = GUICtrlCreateInput("", 10, 65, 100, 20, $ES_NUMBER)
$InputID[1] = GUICtrlCreateInput("", 225, 90, 85, "", $ES_READONLY)
GUICtrlCreateLabel("...", 10, 90)
GUICtrlCreateLabel("...", 10, 115)
$Calculate = GUICtrlCreateButton("...", 325, 90, 60, 20, $BS_VCENTER + $BS_CENTER)
;GUICtrlSetOnEvent($Calculate, "_Calculate")
$Settings = GUICtrlCreateButton("...", 325, 110, 60, 20, $BS_VCENTER + $BS_CENTER)
;GUICtrlSetOnEvent($Settings, "_Settings")
GUICtrlCreateLabel("...", 10, 155)
$Checkbox[0] = GUICtrlCreateCheckbox("...", 10, 175)
$Checkbox[1] = GUICtrlCreateCheckbox("...", 10, 195)
$Checkbox[2] = GUICtrlCreateCheckbox("...", 10, 215)
$Checkbox[3] = GUICtrlCreateCheckbox("...", 10, 235)
$Checkbox[4] = GUICtrlCreateCheckbox("...", 10, 255)
$Checkbox[5] = GUICtrlCreateCheckbox("...", 10, 275)
$Checkbox[6] = GUICtrlCreateCheckbox("...", 10, 295)
$Checkbox[7] = GUICtrlCreateCheckbox("...", 10, 315)
$CheckAll = GUICtrlCreateButton("Check all", 315, 175, 70, 20, $BS_VCENTER + $BS_CENTER)
;GUICtrlSetOnEvent($CheckAll, "_CheckAll")
$UncheckAll = GUICtrlCreateButton("Check none", 315, 195, 70, 20, $BS_VCENTER + $BS_CENTER)
;GUICtrlSetOnEvent($UncheckAll, "_UncheckAll")
GUICtrlCreateLabel("...", 10, 370)
While 1
    $nMsg = GUIGetMsg(0)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Calculate
            _Calculate()
        Case $Settings
            _Settings()
        Case $CheckAll
            _CheckAll()
        Case $UncheckAll
            _UnCheckAll()
    EndSwitch
WEnd

; -----------------------------------------------
; --------------- main functions! ---------------
; -----------------------------------------------

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

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

Func _Settings()
    $hSettings = GUICreate("Settings", 350, 350, 325, 250, $WS_OVERLAPPEDWINDOW)
    WinSetOnTop("Settings", "", 1)
    GUISetState(@SW_SHOW)
    While 1
        $aMsg = GUIGetMsg(1)
        Switch $aMsg[1]
            Case $hSettings
                Switch $aMsg[0]
                    Case $GUI_EVENT_CLOSE
                        GUIDelete($hSettings)
                        Return
                EndSwitch
        EndSwitch
    WEnd
EndFunc   ;==>_Settings

Func _DrawLine()
    _GDIPlus_Startup ()
    $Graphic = _GDIPlus_GraphicsCreateFromHWND ($GUI)
    $Pen = _GDIPlus_PenCreate ("", 20, 2)
    _GDIPlus_GraphicsDrawLine ($Graphic, 10, 40, 375, 40)
    _GDIPlus_PenDispose ($Pen)
    _GDIPlus_GraphicsDispose ($Graphic)
    _GDIPlus_ShutDown ()
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _DoNothing()
EndFunc   ;==>_DoNothing

Func _Calculate()
EndFunc

Note also the use of GUIGetMsg with the "advanced" parameter when you open your Settings GUI. That way you can identify the GUI sending the message. :party:

Ask if anything is unclear. :mellow:

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
pl123

Thanks for helping me, both of you. I get what you're saying.

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  

×