Jump to content

MsgLoop - SetState of Ctrl AND perform additional actions


AnAlien
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

I'm new to AutoIt so this is going to be a little tough to explain. Please ask for clarification if needed.

So in the message loop mode, you GUI is polled constantly and you have switch statements to determine which control was clicked on. 

I have multiple check boxes, and at the end, there is a "Select All" check box which selects all checkboxes (so complex, right?). 

So depending on the checkbox selected, it's supposed to update multiple values/variables, and reset them if unchecked. Also, I want to make sure that certain combination of check boxes are not selected because they are invalid and the end user would be notified if that specific combination is selected (and not allow the program to continue). 

So if:

While 1; msg loop
...
...
Case $chBox1 ;is called, 
    ;I want to perform multiple things
    ;...
    ;..

...
..

Originally, I implemented it so when the $SelectAllCheckBox is checked, it simply checks all other checkboxes by having multiple calls to: GUICtrlSetState($chBox1...) for each checkBox.

But if I simply change the state of the check box using the method above, then I can't do the additional things (without having ugly re-written code).  

How do I DIRECTLY send a msg to the GUI so that when GUIGetMsg() is called, it's the msg I set? Or is there a better way to do this? Should I have used onEvent instead?

Edited by AnAlien
Link to comment
Share on other sites

  • Solution

You shouldn't be looking at the events from clicking on the checkboxes, you should use a button that only performs an action by reading the checked and unchecked checkboxes. You can monitor the CheckAll checkbox, but you shouldn't be monitoring the rest of them, it gets far too complicated when you do it that way.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

AnAlien,

I would do it something like this: :)

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

Global $aCheck[3], $aDummy[3]

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

; Create checkboxes and linked dummy controls
For $i = 0 To UBound($aCheck) - 1
    $aCheck[$i] = GUICtrlCreateCheckbox("Checkbox " & $i, 10, 10 + (40 * $i), 200, 20)
    $aDummy[$i] = GUICtrlCreateDummy()
Next

$cCheckAll = GUICtrlCreateCheckbox("Check All", 10, 130, 200, 20)

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $cCheckAll
            ; Decide on state to set
            $iState = ( (GUICtrlRead($cCheckAll) = 1) ? ($GUI_CHECKED) : ($GUI_UNCHECKED) )
            ; Loop through the checkboxes
            For $i = 0 To UBound($aCheck) - 1
                ; If we are changing a checkbox state
                If GUICtrlRead($aCheck[$i]) <> $iState Then
                    ; Fire the relevant dummy control
                    GUICtrlSendToDummy($aDummy[$i])
                EndIf
                ; Chenge the state
                GUICtrlSetState($aCheck[$i], $iState)
            Next
            
        Case Else
            For $i = 0 To UBound($aCheck) - 1
                ; Was a ccheckbox changed
                If $iMsg = $aCheck[$i] Then
                    ; Check if valid combination
                    If _Valid_Combination() Then
                        ; Call code to run
                        Call("_Check" & $i)
                    Else
                        MsgBox($MB_SYSTEMMODAL, "Error", "Invalid checkbox combination")
                    EndIf
                    ; No point in looking further
                    ExitLoop
                EndIf
                ; Was s dummy fired
                If $iMsg = $aDummy[$i] Then
                    ; Call code to run
                    Call("_Check" & $i)
                    ; No point in looking further
                    ExitLoop
                EndIf
            Next

    EndSwitch

WEnd

Func _Check0()

    If GUICtrlRead($aCheck[0]) = 1 Then
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 0 checked")
    Else
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 0 unchecked")
    EndIf

EndFunc

Func _Check1()

    If GUICtrlRead($aCheck[1]) = 1 Then
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 1 checked")
    Else
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 1 unchecked")
    EndIf

EndFunc

Func _Check2()

    If GUICtrlRead($aCheck[2]) = 1 Then
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 2 checked")
    Else
        MsgBox($MB_SYSTEMMODAL, "Hi", "Running code for Check 2 unchecked")
    EndIf

EndFunc

Func _Valid_Combination()

    ; Create a strign to match the current checkbox state
    $sValid = ""
    For $i = 0 To UBound($aCheck) - 1
        $sValid &= ( (GUICtrlRead($aCheck[$i]) = 1) ? ("X") : ("-") )
    Next
    ; Now check the validity of that combination
    Switch $sValid
        Case "XXX", "X-X", "X--", "-X-", "--X"
            Return True
    EndSwitch
    Return False

EndFunc
Please ask if you have any questions. :)

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

 

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