Jump to content
Sign in to follow this  
Joboy2k

Func parameters help

Recommended Posts

Hi there, sorry If this is a silly question but I'm either doing something wrong or just misunderstanding how Func parameters work while using GuiCtrlSetOnEvent mode.

 

I have made a mock up script to demonstrate the 2 questions I have.

Question 1: I Know it specifically says in the help file "You can not call a function using parameters" but I was under the impression that the optional parameters had a default value when function is called. Does this not apply with GuiCtrlSetOnEvent?, I.E on startup of this script I call the Function TEST() which uses the default parameter value of "Test String", but when I click the test button on the guy with the OnEvent command I was expecting it to use the default parameter which it doesn't. Is this just how it works or am I doing something wrong?

 

Question 2: Does every Optional parameter variable have to be declared Global first? I was under the impression that putting it in the Func parameter section it would create the variable and it would just work in that Func locally. if I was to remove the line "Global $Test" then I will get "Variable used without being declared" when trying to call the Msgbox. So does that mean in a script with hundreds of Func's I would need to declare every single parameter with a Global at the beginning of the script?

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $Test

Test()

$Gui = GUICreate("Test", 150, 150)
$Button = GUICtrlCreateButton("Test", 50, 50)
GUICtrlSetOnEvent($Button, "Test")
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
GUISetState(@SW_SHOW, $Gui)

Func Test($Test = "Test string")
    MsgBox(0,0,$Test, 3)
EndFunc

Func Close()
    Exit
Endfunc

While 1
    Sleep(100)
WEnd

 

 

Sorry if they are stupid questions and i'm just not understanding the help file properly.

Thank you in advance, i'm sure this will be a simple one for most of you to answer.

 

Share this post


Link to post
Share on other sites
50 minutes ago, joboy2k said:

Does this not apply with GuiCtrlSetOnEvent?,

No it does not. You can not call a function with parameters with GUICtrlSetOnEvent, the parameter variables will not be read, and you will get that error of undeclared variables if you try to access them in the function. Default values set to those unread variables won't be read either.


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

Thank you for your reply. Its helped a lot and now I know I can see why I get issues in some of my programs.

Is there a reason GuiCtrlSetOnEvent was designed to not use the optional parameters? is it so it could be more versatile and still be used on Funcs that have parameters that need to be set? sorry for the follow up question.

 

Thanks in advance.

Share this post


Link to post
Share on other sites

It doesn't use any of the parameters, optional or not. HotKeySet works the same way.

If you absolutely need to use a function that needs parameters, there are ways to do it, but they require more work. For an example see below.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Global $iCancel, $iOK
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")

    $iOK = GUICtrlCreateButton("OK", 10, 30, 50)
    GUICtrlSetOnEvent(-1, "Something_Pressed")

    $iCancel = GUICtrlCreateButton("Cancel", 0, -1)
    GUICtrlSetOnEvent(-1, "Something_Pressed")

    GUISetState(@SW_SHOW)

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

Func Something_Pressed()
    Switch @GUI_CtrlId
        Case $iCancel
            CancelPressed(@GUI_CtrlId)
        Case $iOK
            OKPressed(@GUI_CtrlId)
    EndSwitch
EndFunc   ;==>Something_Pressed

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

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

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

This is a modified version of the example script from the help file that I am using to show how to use a function when using OnEvent mode.

The only reason I would use a function that requires parameters in OnEvent mode would be if the function is used by calling it directly somewhere in the script and it also needs to be used when handling an event. This is very rare, and can easily be avoided.


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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...