Sign in to follow this  
Followers 0
Nahuel

Problem with GuiOnEventMode and functions with parameters.

6 posts in this topic

I'm guessing that this behaviour is intended, but could someone explain why?

If I create a function with just one optional parameter and this function is called from an event control, it works as long as I don't use the optional parameter inside the function. Here's an example:

Opt("GUIOnEventMode",1)

 GUICreate("Test",200,100)
 GUICtrlCreateButton("no param",0,0,100,100)
 GUICtrlSetOnEvent(-1,"testfunc_1")
 
 GUICtrlCreateButton("with param",100,0,100,100)
 GUICtrlSetOnEvent(-1,"testfunc_2")
 
 GUISetState()
 
 While 1
     Sleep(100)
 WEnd
 
 Func testfunc_1($optparam="")
    MsgBox(0,"","Works! didn't use the optional parameter inside function")
 EndFunc
  Func testfunc_2($optparam="")
     If $optparam="" Then Return MsgBox(0,"","Empty string!")
     MsgBox(0,"",$optparam)
 EndFunc

I know that GUiCtrlSetOnEvent() can't call a function with parameters, but why are they totally ignored even if they are optional? It's like parameter variables are not declared when the udf is registered for an onevent call?

I wanted to create a function that could be called without parameters from a control and with parameters from other parts of the script. Of course I came up with a workaround, so this isn't really a problem but a curiosity.

Share this post


Link to post
Share on other sites

Hello my friend ;)

I bet you didn't think of this way to script it:

Opt("GUIOnEventMode",1)

GUICreate("Test",200,100)
GUICtrlCreateButton("Function Call",0,0,100,100)
GUICtrlSetOnEvent(-1,"testfunc_1")

GUICtrlCreateButton("Normal Call",100,0,100,100)
GUICtrlSetOnEvent(-1,"testfunc_2")

GUISetState()
Global $optparam, $2ndparam
While 1
     Sleep(100)
WEnd

Func testfunc_1()
    testfunc_2("1st parameter", "2nd parameter")
EndFunc
Func testfunc_2($optparam = "", $2ndparam = "")
    If $optparam = "" And $2ndparam = "" Then 
        MsgBox(0,"Button Click","Empty string!")
    Else
        MsgBox(0,"Function call",$optparam & "   "&$2ndparam)
    EndIf
EndFunc

So - it can be done - you can use the function without parameters in the normal call (button press) and with parameters (function call).

I had no idea if it were possible but you've made me think about it :D hehe - we both learned something out of this ;)


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

enaiman

I think will be better declare variable

Dim $optparam

Opt("GUIOnEventMode",1)

GUICreate("Test",200,100)
GUICtrlCreateButton("no param",0,0,100,100)
GUICtrlSetOnEvent(-1,"testfunc_1")

GUICtrlCreateButton("with param",100,0,100,100)
GUICtrlSetOnEvent(-1,"testfunc_2")

GUISetState()

While 1
    Sleep(100)
WEnd

Func testfunc_1($optparam = "")
    MsgBox(0,"","Works! didn't use the optional parameter inside function")
EndFunc

 Func testfunc_2($optparam = "")
    If $optparam = "" Then Return MsgBox(0,"","Empty string!")
    MsgBox(0,"",$optparam)
EndFunc

Share this post


Link to post
Share on other sites

Thank you guys! This wasn't really a problem, but a question. I thought it was weird, that's all. Martin's way is the best solution, IMO. I tried declaring the variable before but I got the "variable already declared as parameter" error.

I wonder why this happens...

Share this post


Link to post
Share on other sites

It doesn't happen Nahuel - I guess you haven't tried my example or Rasim's; both of them work. You have to declare the variable first but after that everything is fine.

Have a look at our examples.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

It seems that I can't make myself clear :)

I wasn't looking for a solution, I said I already have one. I just thought it was weird.

Thank you guys for replying >_<

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