Search code examples
autoit

Ask about Radio in Autoit


How to put $Day into $Radio1 , $Week into $Radio2, $Month into $Radio3

And - $Radio1,$Radio2,$Radio3 into $RadioCheck ?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
GUICreate("Example", 143, 103, 192, 124)
GUISetFont(12, 400, 0, "Open Sans")
$Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17)
$Radio2 = GUICtrlCreateRadio($Week, 24, 40, 113, 17)
$Radio3 = GUICtrlCreateRadio($Month, 24, 64, 113, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $RadioCheck = $Radio1,$Radio2,$Radio3 ; The problem is here
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
Func Example1()
    Local $Day, $Week, $Month
    $LimitTime =  _DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ;Get the current time (convert to seconds)
    $Day = 86400 ; total seconds a Day
    $Week = 604800 ; total seconds a Week
    $Month = 2592000 ; total seconds a Month
   _Example2($Example3, $Example4, $Example5-$RadioCheck)
EndFunc

Solution

  • Your $Day, $Week and $Month are local variables (inaccessible from global scope). More importantly: you are trying to set the text of your radio controls before you have declared or initialized these variables.

    So how do you solve that? You have (at least) three options:

    Option 1

    Change your local variables from your function Example1() to global scope.

    • So change: Local $Day, $Week, $Month to Global $Day, $Week, $Month,
    • put it on top of your script and then
    • call the function Example1() before you create the GUI!

    This is probably the easiest, yet dirtiest way, since any function could at any time change the data of your variables. In general try not to use global variables if possible.

    Option 2

    Change your radio controls to global variables and then change their text inside your Example1() function. Like this:

    $Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17)
    $Radio2 = ...
    

    to

    Global $Radio1 = GUICtrlCreateRadio("", 24, 16, 113, 17)
    Global $Radio2 = GUICtrlCreateRadio("", 24, 40, 113, 17)
    Global $Radio3 = GUICtrlCreateRadio("", 24, 64, 113, 17)
    

    Note that you have to remove your undeclared variables! Then change the text of the controls in your Example1() function using:

    GUICtrlSetData($Radio1, $Day)
    GUICtrlSetData($Radio2, $Month)
    GUICtrlSetData($Radio3, $Week)
    

    Option 3a

    This is the safest way. Let Example1() return the variables, either ByRef, or create and return an array. As array (untested):

    #include <ButtonConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    
    ; We are now going to receive the return values (in this case an array) from our function:
    Local $aDate = Example1() ; it is essential to call this function before we want to make use of $Date[0] to $Date[2].
    ; What happens is that the function (code block further down) named Example1() is being executed.
    ; This function will then RETURN us the array.
    ; Since Example1() returns an array, $aData will automatically become an array filled with the data of Example1()
    
    #Region ### START Koda GUI section ### Form=
    GUICreate("Example", 143, 103, 192, 124)
    GUISetFont(12, 400, 0, "Open Sans")
    $Radio1 = GUICtrlCreateRadio($aDate[0], 24, 16, 113, 17) ; we are now setting the data for the three controls returned by Example1()
    $Radio2 = GUICtrlCreateRadio($aDate[1], 24, 40, 113, 17)
    $Radio3 = GUICtrlCreateRadio($aDate[2], 24, 64, 113, 17)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    ; Local $RadioCheck = $Radio1,$Radio2,$Radio3 ; The problem is here ---- You do not need that and PLEASE READ THE ABOUT LOCAL AND GLOBAL VARIABLES!
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
    
    Func Example1()
        Local $aReturn[3] ;create a (Local) Array
        $LimitTime =_DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ; Get the current time (convert to seconds)
        $aReturn[0] = 86400
        $aReturn[1] = 604800
        $aReturn[2] = 2592000
        Return $aReturn ; return the Array
    EndFunc
    

    Option 3b

    You could also return the values ByRef :

    #include <ButtonConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    
    Local $Day, $Week, $Month ; we have to declare the variables for our Example1() function BEFORE we use them
    Example1($Day, $Week, $Month) ; we here call our function with the previously declared variables as parameter.
    ; The function will then fill in the data into our variables before we use them to set the radio text
    
    GUICreate("Example", 143, 103, 192, 124)
    GUISetFont(12, 400, 0, "Open Sans")
    $Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17) ; set the data for the three controls
    $Radio2 = GUICtrlCreateRadio($Week, 24, 40, 113, 17)
    $Radio3 = GUICtrlCreateRadio($Month, 24, 64, 113, 17)
    GUISetState(@SW_SHOW)
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
    
    Func Example1(ByRef $Day, ByRef $Week, ByRef $Month) ; NOTE: ByRef means that the given variables will be OVERWRITTEN, that also means that the variables MUST EXIST before the function is called
        $LimitTime =  _DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ; Get the current time (convert to seconds)
        $Day = 86400
        $Week = 604800
        $Month = 2592000
    EndFunc
    

    Sources