# Function Reference

# GUICtrlSetOnEvent

Defines a user-defined function to be called when a control is clicked.

GUICtrlSetOnEvent ( controlID, "function" )

## Parameters

controlID | The control identifier (controlID) as returned by a GUICtrlCreate...() function, or -1 for the last created control.
function | The name of the user function to call.

## Return Value

Success: | 1.
Failure: | 0.

## Remarks

OnEvent functions are only called when the option GUIOnEventMode is set to 1 - when in this mode GUIGetMsg() is NOT used at all.
You can not call a function using parameters.
Within the called user function the control identifier can be retrieved with @GUI_CtrlId.
If needed the windows handle and the control handle can be retrieved with @GUI_WinHandle or @GUI_CtrlHandle.
If the function is an empty string "" the previous user-defined is disabled.

## Related

GUICtrlCreate..., GUICtrlCreateDummy, GUICtrlSendToDummy, GUIGetMsg, GUIOnEventMode (Option), GUISetOnEvent

## Example

    #include <GUIConstantsEx.au3>
    #include <MsgBoxConstants.au3>

    Example()

    Func Example()
        Opt("GUICoordMode", 2)
        Opt("GUIResizeMode", 1)
        Opt("GUIOnEventMode", 1)

        GUICreate("Parent1")
        GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
        GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
        GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

        GUICtrlCreateButton("OK", 10, 30, 50)
        GUICtrlSetOnEvent(-1, "OKPressed")

        GUICtrlCreateButton("Cancel", 0, -1)
        GUICtrlSetOnEvent(-1, "CancelPressed")

        GUISetState(@SW_SHOW)

        ; Just idle around
        While 1
            Sleep(10)
        WEnd
    EndFunc   ;==>Example

    Func OKPressed()
        MsgBox($MB_SYSTEMMODAL, "OK Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
    EndFunc   ;==>OKPressed

    Func CancelPressed()
        MsgBox($MB_SYSTEMMODAL, "Cancel Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
    EndFunc   ;==>CancelPressed

    Func SpecialEvents()
        Select
            Case @GUI_CtrlId = $GUI_EVENT_CLOSE
                MsgBox($MB_SYSTEMMODAL, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
                Exit

            Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
                MsgBox($MB_SYSTEMMODAL, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

            Case @GUI_CtrlId = $GUI_EVENT_RESTORE
                MsgBox($MB_SYSTEMMODAL, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

        EndSelect
    EndFunc   ;==>SpecialEvents