Sign in to follow this  
Followers 0
Muffy1979

Disable check box from a seperate a function

4 posts in this topic

Hi,

I am Completely new to Autoit(24hrs) the last time I did any kind of programing was over 10yrs ago using visual basic, I have got this far by pure and simple luck and hours of research.

Please excuse my lack of correct terminology but I will explain my requirements as best as i can.

I need a GUI that will allow me to check/uncheck a checkbox (Pushlike) then will enable/disable further checkboxes accordingly. I have managed to get this working exactly as I need it but when I place the main body of the script into a function call it no longer works.

I have rewritten a simpler version (below) of exactly what my problem is as my main script is now 2k lines long and is getting a tad complicated.

As you can see if i comment out the function commands it works perfectly but not with them in place as I require.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>

Opt("GUIOnEventMode", 1)


;Call ("main")

;-----------------------------------------------------------------------------
;-----------------------The following must be within a function call----------
;-----------------------------------------------------------------------------

;Func main()
        $mycheckboxtest = GUICreate("Test", 180, 130, 100, 100)
                    $Checkbox1 = GUICtrlCreateCheckbox("Variable 1", 24, 10, 80, 25, $BS_PUSHLIKE)
                    $Checkbox2 = GUICtrlCreateCheckbox("Variable 2", 24, 40, 80, 25)
                    GUICtrlSetState($Checkbox2, $GUI_disable)
                    $Checkbox3 = GUICtrlCreateCheckbox("Variable 3", 24, 70, 80, 25)
                    GUICtrlSetState($Checkbox3, $GUI_disable)
                    $Button1 = GUICtrlCreateButton("&OK", 10, 100, 75, 25, $WS_GROUP)
                    $Button2 = GUICtrlCreateButton("&Cancel", 100, 100, 75, 25, $WS_GROUP)
            GUISetOnEvent($GUI_EVENT_CLOSE, "Cancel")
            GUICtrlSetOnEvent($Button1, "Cancel")
            GUICtrlSetOnEvent($Button1, "Cancel")
            GUICtrlSetOnEvent($Checkbox1, "Control")
            GUICtrlSetOnEvent($Checkbox2, "Variable")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd
;EndFunc

;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------

Func cancel()
Exit
EndFunc

Func control()
    if GUICtrlRead($Checkbox1) = $GUI_UNCHECKED Then
                        GUICtrlSetState($Checkbox2, $GUI_disable)
                        GUICtrlSetState($Checkbox3, $GUI_disable)
        ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                        GUICtrlSetState($Checkbox2, $GUI_enable)
                        GUICtrlSetState($Checkbox3, $GUI_enable)
        EndIf
EndFunc

Share this post


Link to post
Share on other sites



Because your checkboxes are assigned to variables that are contained in the Function Main(), those variables are given a local scope. That means that they're not accessible from outside that function. You need to declare them as Global, either inside the Main function, or at the top of the script which is the usual practice. Once they are declared as global variables, your script works like I think you're trying to get it to work.

BTW, if you want to call a specific function, you don't need to use the Call function, you can call it like this:

Main()

The only time I would think you'd need to use the Call("Function name") function would be if you're dynamically calling a function by a name contained in a variable that gets changed somewhere along the line.


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

Share this post


Link to post
Share on other sites

AWESOME!!!! sorted it.....thanks for the help and tip ;)

Share this post


Link to post
Share on other sites

You're welcome.

And welcome to the forum.


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

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