Jump to content

How to tell $msg what checkbox is checked?


 Share

Recommended Posts

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
Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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