Sign in to follow this  
Followers 0
Wooltown

Send paramaters to the function specified in GUICtrlSetOnEvent

11 posts in this topic

Hello !

Is it possible to make changes in GUICtrlSetOnEvent so you can send parameters with the function ?

For example: GUICtrlSetOnEvent ( controlID, "function(ByRef $Value)" )

Regards

/Sven

Share this post


Link to post
Share on other sites



Hello !

Is it possible to make changes in GUICtrlSetOnEvent so you can send parameters with the function ?

For example: GUICtrlSetOnEvent ( controlID, "function(ByRef $Value)" )

Regards

/Sven

The idea seems cool at a first glance but I don't know how this would really work. The parameter cannot refer to a local variable, as the callback funciton is called outside of the regular flow of the program. Thus it would need to be a global variable. But if it is a global variable, you could just declare the global variable in your Callback function and simply use it, you do not need to get it as a function parameter.

Something less ambitious and perhaps more useful would be to be able to pass , but which would be already pretty useful, is to remove the limitation of only passing functions as callbacks. What I'd like would be to be able to pass any string that could be passed to "Eval". This would not add functionnality per se, but it would be a very nice way of making code more compact. For instance if I want to do "Exit 1" when I click ka button, I need to create function and in it put the Exit 1 statement:

GUICtrlSetOnEvent ( btnExit, "my_exit" )

...

Func my_exit

Exit 1

EndFunc

Instead I'd love to be able to directly say:

GUICtrlSetOnEvent ( btnExit, "Exit 1" )

Or perhaps:

GUICtrlSetOnEvent ( btnExit, "Eval", "Exit 1" )

It would make code easier to read, in my opinion. It would also reduce the need of using global variables to be able to reuse functions in several callbacks.

This does not have (IMHO) the same problem that what you propose has and it would cut down the need for global variables a lot.

Cheers,

Angel

Share this post


Link to post
Share on other sites

Hello !

The solution you suggested can solve my problem, then I can use one Function instead of several, when to toggle around the values of run mode.

GUICtrlSetOnEvent ( btnMode, "MyFunction Full" )´

Func Myfunction($runmode)

$result = IniWrite ("G:\INIfiles\" & @ComputerName & ".ini","Startup","LockRunStatus",$runmode)

EndFunc

/Regards

Sven

Share this post


Link to post
Share on other sites

I need to do something similar to this. Any solution?

Share this post


Link to post
Share on other sites

This is a crude work around example of something I've used in the past:

#include <GUIConstants.au3>
Opt('GUIOnEventMode', 1)

Dim $VarToPass; have to dim the variable your going to use as a parameter
Dim $New

$Main = GUICreate('MyGui', 200, 100)
$Button1 = GUICtrlCreateButton('Pass Var', 60, 30, 70, 30)
GUICtrlSetOnEvent($Button1, "PassVar"); Example would be like:  GUICtrlSetOnEvent($Button1, 'PassVar', $VarToPass)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitNow")

GUISetState()

While 1
    Sleep(10)
    $New = $New + 1
    If $New = 11 Then 
        $VarToPass = $New
    EndIf
WEnd

Func PassVar(); << dummy function so you can pass a variable with set on event
    PassingVar($VarToPass)
EndFunc

Func PassingVar($Value); << this is the actual function I would have normally have called instead of the above work around
    MsgBox(0, '', $Value)
EndFunc

Func ExitNow()
    Exit
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I had the same issue a while back. My example in the forums was dealing with hotkeys instead of OnEvents, but my script had both. The principle is the same though, that when called, hotkey or onEvent functions cannot have any parameters at all, and if the function being called even has optional parameters, it won't work. Here's the link for some more info.

http://www.autoitscript.com/forum/index.php?showtopic=18732

The overall result is the same - use a dummy function for the hotkey/onEvent call, and then supply the real function with the needed variables.

Share this post


Link to post
Share on other sites

I had the same issue a while back. My example in the forums was dealing with hotkeys instead of OnEvents, but my script had both. The principle is the same though, that when called, hotkey or onEvent functions cannot have any parameters at all, and if the function being called even has optional parameters, it won't work. Here's the link for some more info.

http://www.autoitscript.com/forum/index.php?showtopic=18732

The overall result is the same - use a dummy function for the hotkey/onEvent call, and then supply the real function with the needed variables.

Ha... surprised I hadn't seen that thread.... but yes.. same concept.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Thanks. The variable I wanted to pass was the button number pressed and because I was continually creating and destroying the buttons in different GUI windows, I couldn't just use @GUI_CrlId on its own to reference the buttons (sometimes the same ID was re-used for different buttons). I got around it by doing the following:

GUICtrlSetOnEvent($hButton[$i], "RunProg"); where $i is the button number

Func RunProg()
For $i = 1 to $iLastButton
    If @GUI_CtrlId = $hButton[$i] Then ExitLoop
Next

....

EndFunc

By knowing the exact value of $i (and hence, what button was pressed), I was able to use a single function rather than a separate function for every button created.

Still, it would be nice if GUICtrlSetOnEvent could pass variables to functions.

Share this post


Link to post
Share on other sites

Thanks. The variable I wanted to pass was the button number pressed and because I was continually creating and destroying the buttons in different GUI windows, I couldn't just use @GUI_CrlId on its own to reference the buttons (sometimes the same ID was re-used for different buttons). I got around it by doing the following:

GUICtrlSetOnEvent($hButton[$i], "RunProg"); where $i is the button number

Func RunProg()
For $i = 1 to $iLastButton
    If @GUI_CtrlId = $hButton[$i] Then ExitLoop
Next

....

EndFunc

By knowing the exact value of $i (and hence, what button was pressed), I was able to use a single function rather than a separate function for every button created.

Still, it would be nice if GUICtrlSetOnEvent could pass variables to functions.

Try this. I hope it helps you.

Global $hButton[2][5]
For $i = 0 To 4
   $hButton[0][$i] = GUICtrlCreateButton(your values...)
   $hButton[1][$i] = 'identifier for your program or full path if you wish'
   GUICtrlSetOnEvent($hButton[0][$i], "RunProg"); where $i is the button number
Next

Func RunProg()
   Local $Program
   For $i = 0 To 4
      If @GUI_CtrlId = $hButton[0][$i] Then
         $Program = $hButton[1][$i]
         ExitLoop
      EndIf
   Next

   If $Program <> '' Then Run($Program)

EndFunc

Share this post


Link to post
Share on other sites

That's a really good idea you 2 (PartyPooper/SlimShady)... Nice!!


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Good idea Slim - I hadn't thought to use a multi-dimensional array.

Thanks Smoke. I use this type of thing in my FastGaming program (download the source and check it out if you're interested).

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