Jump to content

Problem with Checkboxes and Button


 Share

Recommended Posts

Hi,

I am having a problem with the code, i want to do is have checkbox selected and press one button it will take me to another function, can someone help me.

#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
 
main()
Func main()
Dim $id[10], $var[10]
Dim $button1
Local $avArray[10]
$avArray[1] = "C2S VPN Over Internet"
$avArray[2] = "S2S VPN Over Internet"
Local $msg
GUICreate("Script Automator", 400, 150)
For $t = 1 To 2
$var[$t] = GUICtrlRead(GUICtrlCreateCheckbox($avArray[$t], 10, 10 * $t * 2, 200, 20), 1)
Next
$button1 = GUICtrlRead(GUICtrlCreateButton("RUN", 150, 100, 100, 30))
 
GUISetState()
Do
$msg = GUIGetMsg()
If $msg = $button1 Then
For $t = 1 To 2
If _IsChecked($var[$t]) Then
RUN_APP()
EndIf
Next
EndIf
Until $msg = $GUI_EVENT_CLOSE
EndFunc
 
Func _IsChecked($control)
Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc
 
Func RUN_APP()
MsgBox(0, "test", "Voila")
Return
EndFunc
 
Regards,
Oldwine
Link to comment
Share on other sites

First, a tip.  Please post code in AutoIt code tags.  It makes it far more readable.

Now, you've got a couple problems here.

1 - You define $button1 as the read value of your gui's button (in other words, the text of the button).  You want to define $button1 as the button itself.

2 - You prefill your array with the read values of the checkboxes and never update it.  You need to instead read the checkbox value after the button is clicked to make sure that it has current data.  Depending on how many checkboxes you're going to use, you may not even need the array (just do GUICtrlRead twice).

Link to comment
Share on other sites

Have a look

#include <GUIConstantsEx.au3>

main()

Func main()

    Local $avArray[10] = ["CheckBox 1", "CheckBox 2"], $button1

    Local $msg, $CheckBox[2]
    GUICreate("Script Automator", 400, 150)
    For $t = 1 To 2
        $CheckBox[$t - 1] = GUICtrlCreateCheckbox($avArray[$t - 1], 10, 10 * $t * 2, 200, 20)
    Next
    $button1 = GUICtrlCreateButton("RUN", 150, 100, 100, 30)

    GUISetState()
    Do
        $msg = GUIGetMsg()
        Switch $msg
            Case $button1
                If _IsChecked($CheckBox[0]) Then
                    ConsoleWrite("CheckBox 1 is checked" & @CRLF)
                    ;execute your rest functions that should be executed when Checkbox 1 is checked
                EndIf

                If _IsChecked($CheckBox[1]) Then
                    ConsoleWrite("CheckBox 2 is checked" & @CRLF)
                    ;execute your rest functions that should be executed when Checkbox 2 is checked
                EndIf

                ConsoleWrite("-------------------------" & @CRLF)
        EndSwitch
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>main

Func _IsChecked($control)
    Return BitAND(GUICtrlRead($control), $GUI_CHECKED)
EndFunc   ;==>_IsChecked

You never stored the ControlID of the controls returned when you execute GUICtrlCreate...

Rather you stored the values of their state or name using GUICtrlRead

GUICtrlRead should be used when the present state or data of the control has to be known for that particular instant.

As a tip don't use Dim for declaring variables its decremented now, use Local or Global instead.

Regards :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Thank you very much PhoenisXL and Artisan for your valuable insight.

i was trying to use GUICtrlGetHandle to get the handle of the checkboxes and button and apply logic to it, it can be done that way too right?

Regards,

Oldwine.

Link to comment
Share on other sites

Before you guys posted i did it like this,

#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
 
main()
Func main()
Local $avArray[10], $t, $var[10]
$avArray[1] = "C2S VPN Over Local Facility Internet"
$avArray[2] = "S2S VPN Over Local Facility Internet"
Local $msg
GUICreate("Script Automator", 400, 150)
For $t = 1 To 2
$var[$t] = GUICtrlCreateCheckbox($avArray[$t], 10, 10 * $t * 2, 200, 20)
Next
$button1 = GUICtrlCreateButton("RUN", 50, 100, 100, 30)
$button2 = GUICtrlCreateButton("EXIT", 248, 100, 100, 30)
GUICtrlSetBkColor($button2, 0x00ff00)
GUICtrlSetBkColor($button1, 0xE0FFFF)
 
GUISetState()
Do
$msg = GUIGetMsg()
if $msg = $button2 Then
ExitLoop
ElseIf $msg = $button1 Then
if _IsChecked($var[1]) Then
RUN_APP_1()
ElseIf _IsChecked($var[2]) Then
RUN_APP_2()
EndIf
EndIf
Until $msg = $GUI_EVENT_CLOSE
EndFunc
 
Func _IsChecked($control)
Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc
 
Func RUN_APP_1()
MsgBox(0, "test", "C2S VPN CONFIG")
Exit
EndFunc
 
Func RUN_APP_2()
MsgBox(0, "test", "S2S VPN CONFIG")
Exit
EndFunc
 
 
it works...:-)....thanks again guys once again.
u guys are awesome.
 
Regards,
Oldwine.
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...