Jump to content

Help With CheckBoxes


Recommended Posts

Hello All,

This is my first post, and i have only been using autoit since Friday, no other programming experience.

Im building a program and i ran into a problem.  

Basically, i have a GUI with 8 checkboxes and a "Test Button" on it.  When i press the Test button, i want to send the script to a certain function depending on what checkbox is checked, and also display a message box with "ok" and "Cancel"  that states what it is about to test. (Obviously i want the message box to ultimately determine if the script gets sent to the function. If cancel is pressed i want to be able to choose a different checkbox from the GUI.

Here is my code. 

#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#Include <SetScreen.au3>
#include <StaticConstants.au3>
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
    GLobal $ModelWindow = GUICreate ("Select Model", 450,700)
    GUISetFont (14,400)
    GUICtrlCreateLabel("Totoku",190,20)
    GUICtrlCreateLabel("USEI",220,400)
    GUISetFont (10,400)
    GUiCtrlCreateLabel ("Models", 20,50)
    GUISetFont (8.5,400)
    
    Global $Testbutton = GUICtrlCreateButton("Test",345,662,100,30)

    Global $53i2 = GUICtrlCreateCheckBox("MS53i2",40,70)
    Global $51i2 = GUICtrlCreateCheckBox("MS51i2",140,70)
    Global $33i2 = GUICtrlCreateCheckBox("MS33i2",240,70)
    Global $31i2 = GUICtrlCreateCheckBox("MS31i2",340,70)
    Global $551i2 = GUICtrlCreateCheckBox("ME551i2",40,110)
    Global $355i2 = GUICtrlCreateCheckBox("ME355i2",140,110)
    Global $356i2 = GUICtrlCreateCheckBox("CCL356i2",240,110)
    Global $354i2 = GUICtrlCreateCheckBox("CCL354i2",340,110)


GUICtrlSetOnEvent($Testbutton, "Testbutton")
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
Func CLOSEClicked()
    If @GUI_WINHANDLE = $ModelWindow Then
    Exit
    EndIf
EndFunc
GUISwitch ($ModelWindow)
GUISetState(@SW_SHOW)
$i = 0
While 1
    Sleep(1000)  ; Idle around
WEnd

Func Testbutton ()
    Global $MCB1 = GUICtrlRead($53i2)
    Global $MCB2 = GUICtrlRead($51i2)
    Global $MCB3 = GUICtrlRead($33i2)
    Global $MCB4 = GUICtrlRead($31i2)
    Global $MCB5 = GUICtrlRead($551i2)
    Global $MCB6 = GUICtrlRead($355i2)
    Global $MCB7 = GUICtrlRead($356i2)
    Global $MCB8 = GUICtrlRead($354i2)

 


EndFunc


Func FiveMegaRes ()
    MsgBox (0,"","Five");Temp to see if scrpt makes it here
EndFunc

Func ThreeMegaRes ()
MsgBox (0,"","Three")
EndFunc

I have been trying to figure this out for half the day.  I tried many different things but i just cant seem to get it to work properly.  

I think i need to make the checkboxes into an array, but i have absolutely no clue how to do that after hours of searching and trying different things.

Any and all help is greatly appreciated.

*Edit: Sorry if the code is poorly organized.  I am a noob.......

Edited by JohnnyVolcom5
Link to comment
Share on other sites

Hi Johnny,

Welcome to the Forum!

This should be enough framework for you to play with for your current checkbox project:

#include <GUIConstantsEx.au3>

    GLobal $ModelWindow = GUICreate ("Select Model", 450,700)
    GUISetFont (14,400)
    GUICtrlCreateLabel("Totoku",190,20)
    GUICtrlCreateLabel("USEI",220,400)
    GUISetFont (10,400)
    GUiCtrlCreateLabel ("Models", 20,50)
    GUISetFont (8.5,400)

    Global $Testbutton = GUICtrlCreateButton("Test",345,662,100,30)

    Global $53i2 = GUICtrlCreateCheckBox("MS53i2",40,70)
    Global $51i2 = GUICtrlCreateCheckBox("MS51i2",140,70)
    Global $33i2 = GUICtrlCreateCheckBox("MS33i2",240,70)
    Global $31i2 = GUICtrlCreateCheckBox("MS31i2",340,70)
    Global $551i2 = GUICtrlCreateCheckBox("ME551i2",40,110)
    Global $355i2 = GUICtrlCreateCheckBox("ME355i2",140,110)
    Global $356i2 = GUICtrlCreateCheckBox("CCL356i2",240,110)
    Global $354i2 = GUICtrlCreateCheckBox("CCL354i2",340,110)

Do
    GuiSetState()
    $msg = GUIGetMsg()
    Select
        case $msg = $Testbutton
            If GUICtrlRead($53i2) = $GUI_CHECKED Then
                Msgbox(0,"Info","53i2 checked")
                ;call another function here if you wish)
            EndIf
            If GUICtrlRead($51i2) = $GUI_CHECKED Then
                Msgbox(0,"Info","51i2 checked")
                ;call another function here if you wish)
            EndIf
            ;continue other if here
        case $msg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
Until False

 

And yes, it will be much better if you convert those checkbox into array

Link to comment
Share on other sites

Hi Johnny,

Welcome to the Forum!

This should be enough framework for you to play with for your current checkbox project:

#include <GUIConstantsEx.au3>

    GLobal $ModelWindow = GUICreate ("Select Model", 450,700)
    GUISetFont (14,400)
    GUICtrlCreateLabel("Totoku",190,20)
    GUICtrlCreateLabel("USEI",220,400)
    GUISetFont (10,400)
    GUiCtrlCreateLabel ("Models", 20,50)
    GUISetFont (8.5,400)

    Global $Testbutton = GUICtrlCreateButton("Test",345,662,100,30)

    Global $53i2 = GUICtrlCreateCheckBox("MS53i2",40,70)
    Global $51i2 = GUICtrlCreateCheckBox("MS51i2",140,70)
    Global $33i2 = GUICtrlCreateCheckBox("MS33i2",240,70)
    Global $31i2 = GUICtrlCreateCheckBox("MS31i2",340,70)
    Global $551i2 = GUICtrlCreateCheckBox("ME551i2",40,110)
    Global $355i2 = GUICtrlCreateCheckBox("ME355i2",140,110)
    Global $356i2 = GUICtrlCreateCheckBox("CCL356i2",240,110)
    Global $354i2 = GUICtrlCreateCheckBox("CCL354i2",340,110)

Do
    GuiSetState()
    $msg = GUIGetMsg()
    Select
        case $msg = $Testbutton
            If GUICtrlRead($53i2) = $GUI_CHECKED Then
                Msgbox(0,"Info","53i2 checked")
                ;call another function here if you wish)
            EndIf
            If GUICtrlRead($51i2) = $GUI_CHECKED Then
                Msgbox(0,"Info","51i2 checked")
                ;call another function here if you wish)
            EndIf
            ;continue other if here
        case $msg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
Until False

And yes, it will be much better if you convert those checkbox into array

 

Hey thanks alot for the quick replay and help Michael.  I will play around with it.  Thanks again.

Link to comment
Share on other sites

Ok, so that works and i had done something similar to that earlier in the day, but i wanted the message box to have an "ok " and "cancel" options.  When i change the messagebox flag to 1, both cancel and ok will call the function.  I want only OK to call the function and Cancel to return to the Checkboxes screen.    

I dont think i can add another if then statement for the message box, which is what i have been stuck on forever.  I need there to be a fail-safe in case someone checks the wrong box. 

Link to comment
Share on other sites

This should do what you want:

#include <GUIConstantsEx.au3>

    GLobal $ModelWindow = GUICreate ("Select Model", 450,700)
    GUISetFont (14,400)
    GUICtrlCreateLabel("Totoku",190,20)
    GUICtrlCreateLabel("USEI",220,400)
    GUISetFont (10,400)
    GUiCtrlCreateLabel ("Models", 20,50)
    GUISetFont (8.5,400)

    Global $Testbutton = GUICtrlCreateButton("Test",345,662,100,30)

    Global $53i2 = GUICtrlCreateCheckBox("MS53i2",40,70)
    Global $51i2 = GUICtrlCreateCheckBox("MS51i2",140,70)
    Global $33i2 = GUICtrlCreateCheckBox("MS33i2",240,70)
    Global $31i2 = GUICtrlCreateCheckBox("MS31i2",340,70)
    Global $551i2 = GUICtrlCreateCheckBox("ME551i2",40,110)
    Global $355i2 = GUICtrlCreateCheckBox("ME355i2",140,110)
    Global $356i2 = GUICtrlCreateCheckBox("CCL356i2",240,110)
    Global $354i2 = GUICtrlCreateCheckBox("CCL354i2",340,110)

Do
    GuiSetState()
    $msg = GUIGetMsg()
    Select
        case $msg = $Testbutton
            If GUICtrlRead($53i2) = $GUI_CHECKED Then
                If Msgbox(1,"Info","53i2 checked") = 1 Then Call_52i2()
                ;call another function here if you wish)
            EndIf
            If GUICtrlRead($51i2) = $GUI_CHECKED Then
                Msgbox(0,"Info","51i2 checked")
                ;call another function here if you wish)
            EndIf
            ;continue other if here
        case $msg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
Until False

Func Call_52i2()
    Msgbox(0,"Info", "52i2 function is called")
EndFunc
Link to comment
Share on other sites

JohnnyVolcom5,

The following builds on Michaelslamet's suggestion for using an array.  See comments in the code.

#include <GUIConstantsEx.au3>

    local $ModelWindow = GUICreate ("Select Model", 450,700)
    GUISetFont (14,400)
    GUICtrlCreateLabel("Totoku",190,20)
    GUICtrlCreateLabel("USEI",220,400)
    GUISetFont (10,400)
    local $Testbutton = GUICtrlCreateButton("Test",345,662,100,30)
    GUiCtrlCreateLabel ("Models", 20,50)
    GUISetFont (8.5,400)

    ; the 2D array $aChkBoxes is used to hold the controlid and a string identifier for each checkbox

    local $aChkBoxes[8][2]

    $aChkBoxes[0][0] = GUICtrlCreateCheckBox("MS53i2",40,70)
    $aChkBoxes[0][1] = "MS53i2"
    $aChkBoxes[1][0] = GUICtrlCreateCheckBox("MS51i2",140,70)
    $aChkBoxes[1][1] = "MS51i2"
    $aChkBoxes[2][0] = GUICtrlCreateCheckBox("MS33i2",240,70)
    $aChkBoxes[2][1] = "MS33i2"
    $aChkBoxes[3][0] = GUICtrlCreateCheckBox("MS31i2",340,70)
    $aChkBoxes[3][1] = "MS31i2"
    $aChkBoxes[4][0] = GUICtrlCreateCheckBox("ME551i2",40,110)
    $aChkBoxes[4][1] = "ME551i2"
    $aChkBoxes[5][0] = GUICtrlCreateCheckBox("ME355i2",140,110)
    $aChkBoxes[5][1] = "ME355i2"
    $aChkBoxes[6][0] = GUICtrlCreateCheckBox("CCL356i2",240,110)
    $aChkBoxes[6][1] = "CCL356i2"
    $aChkBoxes[7][0] = GUICtrlCreateCheckBox("CCL354i2",340,110)
    $aChkBoxes[7][1] = "CCL354i2"

Do
    GuiSetState()
    $msg = GUIGetMsg()
    Select
        case $msg = $Testbutton

            ; test each of the checkboxes for a checked state
            ; if checked then
            ;   run the procedure for that checkbox
            ;   uncheck the checkbox

            for $1 = 0 to ubound($aChkBoxes) - 1
                if BitAnd(GUICtrlRead($aChkBoxes[$1][0]),$GUI_CHECKED) = $gui_checked then
                    ConsoleWrite( $aChkBoxes[$1][1] & ' checked' & @LF)
                    _checkbox_procedures($aChkBoxes[$1][1])
                    GUICtrlSetState($aChkBoxes[$1][0],$gui_unchecked)
                endif
            next
        case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
Until False

func _checkbox_procedures($parm1)

    ; confirm action to be carried out

    if msgbox(17,'@@@ Confirm Action @@@','Enter OK to continue with ' & $parm1 & ' processing') <> 1 then return

    ; do each checkboxes respective action

    switch $parm1
        case 'ms53i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'ms51i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'ms33i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'ms31i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'ms33i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'me551i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'me355i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'CCL356i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case 'CCL354i2'
            ConsoleWrite('Doing ' & $parm1 & ' stuff...' & @LF)
        case Else
            ConsoleWrite('...SNAFU...' & @LF)
    endswitch

endfunc

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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