Sign in to follow this  
Followers 0
faldo

How to tell $msg what checkbox is checked?

9 posts in this topic

I know this might be considered a newbie question but i've just learnet arrays and have some trouble applying the functionality to my ideas.

If i have a bunch of checkboxes in a GUI, how can i make it so that $msg catches ANY change in state, without having $msg check every single checkbox for change?

I'm sure there is a way to put the following code into an array... the thing i can't figure out is how to make the GUIgetmsg() to react on ANY box being checked. I was about to post in the GUI-support forum but i think this is an array-problem :P

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Example()
Func Example()
    Local $check1,$check2,$check3,$check4,$check5, $msg
    GUICreate("My GUI Checkbox") ; will create a dialog box that when displayed is centered

    $check1 = GUICtrlCreateCheckbox("CHECKBOX 1", 10, 10, 120, 20)
    $check2 = GUICtrlCreateCheckbox("CHECKBOX 2", 20, 30, 120, 20)
    $check3 = GUICtrlCreateCheckbox("CHECKBOX 3", 50, 60, 120, 20)
    $check4 = GUICtrlCreateCheckbox("CHECKBOX 4", 12, 90, 120, 20)
    $check5 = GUICtrlCreateCheckbox("CHECKBOX 5", 17, 120, 120, 20)
    
    GUISetState()     ; will display an  dialog box with 1 checkbox

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        
        If $msg = $check1 then MsgBox(0, "", "Checkbox1 was checked")
        If $msg = $check2 then MsgBox(0, "", "Checkbox2 was checked")
        If $msg = $check3 then MsgBox(0, "", "Checkbox3 was checked")
        If $msg = $check4 then MsgBox(0, "", "Checkbox4 was checked")
        If $msg = $check5 then MsgBox(0, "", "Checkbox5 was checked")
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc  ;==>Example

Share this post


Link to post
Share on other sites



Using Opt('GuiOnEventMode', 1) and not using a message loop. You can then use GuiCtrlSetOnEvent for each CheckBox. Inside your function you can differentiate between them using the @GUI_CTRLID macro or do some test like:

.
.
.
If BitAND(GUICtrlRead(@GUI_CTRLID), $GUI_CHECKED) Then
...

This way you don't need array at all. If.. applicable. ;]

Share this post


Link to post
Share on other sites

Do you want to make it so it reacts when the checkbox is marked or do you want it so when another button is pressed it tells you wich check boxes are ticked...

if you want it to react to the checkbox being marked then look at GUIOnEvent...I'm no expert but I'm failure sure that has something...

If you want it to know if the box is checked on a seperate event (button etc.) then GUICtrlRead will work.

check out

http://www.autoitscript.com/forum/index.ph...hl=GUI+on+event

MDiesel

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I acctually just wanted to clean up the code abit and create one response to all the checkboxes, in the real script there are a lot more checkboxes. Would it be a bad idea to put a tight for-loop to solve the problem?

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Example()
Func Example()
    Local $check[10], $msg
    GUICreate("My GUI Checkbox"); will create a dialog box that when displayed is centered
    $check[1] = GUICtrlCreateCheckbox("CHECKBOX 1", 10, 10, 120, 20)
    $check[2] = GUICtrlCreateCheckbox("CHECKBOX 2", 20, 30, 120, 20)
    $check[3] = GUICtrlCreateCheckbox("CHECKBOX 3", 50, 60, 120, 20)
    $check[4] = GUICtrlCreateCheckbox("CHECKBOX 4", 12, 90, 120, 20)
    $check[5] = GUICtrlCreateCheckbox("CHECKBOX 5", 17, 120, 120, 20)
    
    GUISetState(); will display an  dialog box with 1 checkbox

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        For $i=1 to 5
            If $msg = $check[$i] then MsgBox(0, "", "Checkbox "&$i&" was checked/unchecked")
        Next
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc;==>Example
Edited by faldo

Share this post


Link to post
Share on other sites

GUIOnEvent > For loop.

Share this post


Link to post
Share on other sites

If i understand it corectly, GUIonEvent needs one line per checkbox right? so if i have 200 checkboxes i'll need 200 GUIonEvent or 4 lines of For-loop code? Are you guys sure there isn't a better way to do it?

Share this post


Link to post
Share on other sites

200 checkboxes? Thats alot of options lol.

But often times noone needs 200 checkboxes, so thats never really an issue. To my knoweledge.. No thats just about it. But I'll look into it in a sec if noone with a bit more experience steps up to the plate.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'm not using 200 checkboxes, that was just an example while hopeing i could use arrays... however, every event that i need to enter for each checkbox is a few lines of code and i have 20 checkboxes... with the for-loop i can remain at about 10 lines for 20 events, with GuiOnEvent i'll need about 200 lines of code to manage.

Hope you understand my point of view :P

Edited by faldo

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You can spit out, "auto-position", and scan plenty of indexed checkboxes without much code.

You could proess the checkboxes in various ways, probably determined by how similar the processing for the chechboxes are to one another.

#include <GUIConstants.au3>
Global $Checkbox[64]
Dim $left = 20, $top = 0
GuiCreate("My Pgm", 340, 380)
For $i = 0 to 63; Create an array of indexed checkboxes
    $top += 20
    If $top > 320 Then
        $top = 20
        $left += 80
    EndIf
    $Checkbox[$i] = GUICtrlCreateCheckbox("Port " & $i, $left, $top)
Next
$Scan_Button = GUICtrlCreateButton("SCAN", 20, 350,40,20)
GUISetState()

;-------------------------------------------------------------------------
While 1
    Switch GUIGetMsg()
        Case $Scan_Button
            Scan_Checkboxes()
        Case -3; $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
Exit

;-------------------------------------------------------------------------
Func Scan_Checkboxes()
    For $i = 0 to 63; Scan all checkboxes
        If GUICtrlRead($CheckBox[$i]) = $GUI_CHECKED Then 
            MsgBox(1,"", "Box " & $i & " is checked.")
            Call("Process_Box" & $i)
        EndIf       
    Next
EndFunc

Func Process_Box1()
    MsgBox(1,"","Im doing what I do when Box 1 is clicked")
EndFunc
Func Process_Box5()
    MsgBox(1,"","Im doing what I do when Box 5 is clicked")
EndFunc
Func Process_Box12()
    MsgBox(1,"","Im doing what I do when Box 12 is clicked")
EndFunc

Edit: A separate process function for each checkbox might not be necessary for you...

Edited by Spiff59

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