Jump to content

Pass argument w/function in GUICtrlSetOnEvent?


 Share

Recommended Posts

http://www.autoitscript.com/autoit3/docs/f...lSetOnEvent.htm

Scoured the online content and couldn't find any forum postings related to it. I'm trying to find a way to pass a path to a function when using GUICtrlSetOnEvent. It seems like it should be able to work but I can't seem to make it work. Can anyone help me out?

If this is my function for running the path (I realize there's uneeded code/unfinished code -- I'm just tyring to get this to work initially):

Func __FileOpen($path="")
    MsgBox(0,"",$path)
    If $path <> "" Then
        ShellExecute($path)
    EndIf
EndFunc

I'm trying to use GUICtrlSetOnEvent to execute the path when clicked, and here's how I'm trying to accomplish this in my loop:

$tf = $fp & "\" & $file
If $file <> "desktop.ini" Then
    If StringRight($file,4) = ".lnk" Or StringRight($file,4) = ".url" Then
        GUICtrlCreateMenuItem(StringTrimRight($file,4),$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
    Else
        GUICtrlCreateMenuItem($file,$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
    EndIf
EndIf

...where $tf is the Total File of the file path and the file name (including extension (theoretically getting the absolute path to the file)). My problem is that if I can't pass an argument to my function, I can't figure out how to make Autoit do what I'm looking to do. When I try to pass an argument to my function (as in the code example above), I get an error stating that __FileOpen($fp) is an unknown function. But again, if I can't pass an arugment to the function inside of GUICtrlSetOnEvent, can anyone point me in the right direction as how I get a path executed on a button click when my buttons are created dynamically in a loop? TIA

My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

Link to comment
Share on other sites

just declare a Global variable for use in the function

Opt("RunErrorsFatal", 0) ; silent error for shellexecute, place at top of script below includes

Global $tf ; global var for use in function, place with rest of global vars at top of script

; declared vars and rest of script here

$tf = $fp & "\" & $file
If $file <> "desktop.ini" Then
    If StringRight($file,4) = ".lnk" Or StringRight($file,4) = ".url" Then
        GUICtrlCreateMenuItem(StringTrimRight($file,4),$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen")
    Else
        GUICtrlCreateMenuItem($file,$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen")
    EndIf
EndIf


Func __FileOpen()
    MsgBox(0,"",$tf)
    If $tf <> "" Then
        ShellExecute($tf)
        SetError(@error) ; set error return from shellexecute
    Else
        SetError(0)
    EndIf
    ;$tf = ""
EndFunc

I see fascists...

Link to comment
Share on other sites

yeah but that won't work. take for instance, my loop is looping through a bunch of files and paths. if i continually reset the a global var to a different value, by the time my program is rendered, each button will all click the exact same path (that of the last value of $tf).... right?

My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

Link to comment
Share on other sites

if you want to reuse the same function for more than one menu item then you need

to use @GUI_CTRLID in the function to identify which menu item was used to run the function

and use more than one global var for path/file

you don't show a loop in your example and only one menu item

i assume you are presetting or user is selecting filename elsewhere in your script?

I see fascists...

Link to comment
Share on other sites

here is the entire functions so u can see how it works:

__DirtoMenu("C:\Documents and Settings\All Users\Start Menu\Programs",$menuAllProgsParent)
Func __DirtoMenu($fp,$menu)
    Dim $arrFile,$newMenu,$val,$fp,$file,$search,$menu,$fileAttrib,$menuTempParent
    $search     = FileFindFirstFile($fp & "\*.*")  

    If $search = -1 Then GUICtrlCreateMenuItem("<Parsing Error>",$menuTempParent)
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        
        $tf = $fp & "\" & $file
        $fileAttrib = FileGetAttrib($tf)
        If @error Then 
            GUICtrlCreateMenuItem("<Parsing Error>",$menuTempParent)
            ExitLoop
        Else
            If StringInStr($fileAttrib, "D") Then
                $newMenu = GUICtrlCreateMenu($file,$menu)
                __DirtoMenu($tf,$newMenu)
            Else
                If $file <> "desktop.ini" Then
                    If StringRight($file,4) = ".lnk" Or StringRight($file,4) = ".url" Then
                        GUICtrlCreateMenuItem(StringTrimRight($file,4),$menu)
                        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
                    Else
                        GUICtrlCreateMenuItem($file,$menu)
                        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
                    EndIf
                EndIf
            EndIf
        EndIf
    WEnd

    FileClose($search)
EndFunc

the function iterates a passed directory and does a recursive loop to auto generate a menu -> submenu structure but when it comes to a file of sorts, I was that button, onclick, to run or shellrun that absolute path. so my problem is trying to store that absolute path in the button or create some intelligent method of managing associating the button name with the path, etc... does that clear it up at all?

Edited by thepip3r

My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

Link to comment
Share on other sites

@all

Would this get you going.

$tf = $fp & "\" & $file
If $file <> "desktop.ini" Then
    If StringRight($file,4) = ".lnk" Or StringRight($file,4) = ".url" Then
        GUICtrlCreateMenuItem(StringTrimRight($file,4),$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
    Else
        GUICtrlCreateMenuItem($file,$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen_tf")
    EndIf
EndIf


Func __FileOpen_tf() 
    __FileOpen($tf)
EndFunc

Func __FileOpen($path="")
    MsgBox(0,"",$path)
    If $path <> "" Then
        ShellExecute($path)
    EndIf
EndFunc

regards

ptrex

Link to comment
Share on other sites

looks like you need a 2d array of path\filenames matched to control ids of menu items

col1 path\filename col2 menuitem control id

then use switch/case with @GUI_CTRLID in your __FileOpen() function

Edit: ok scrub the above suggestion

look at the GUIMenuManagement UDF's

set menuentry number when creating menu items, then

when menu item is clicked and function is run, get ctrlid

and use that to get text from menu item

and shell execute that.

Edited by rover

I see fascists...

Link to comment
Share on other sites

That just won't work:

Opt("GuiOnEventMode", 1)
$hGUI = GUICreate("Test", 400, 300)
GUISetOnEvent(-3, "_Quit")
$menu = GUICtrlCreateMenu("Test")
$fp = "C:\temp"
$file = "TestFile.url"
$tf = $fp & "\" & $file
If $file <> "desktop.ini" Then
    If StringRight($file,4) = ".lnk" Or StringRight($file,4) = ".url" Then
        GUICtrlCreateMenuItem(StringTrimRight($file,4),$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
    Else
        GUICtrlCreateMenuItem($file,$menu)
        GUICtrlSetOnEvent(-1,"__FileOpen_tf")
    EndIf
EndIf
GUISetState()

While 1
    Sleep(20)
WEnd

Func __FileOpen_tf()
    __FileOpen($tf)
EndFunc

Func __FileOpen($path="")
    MsgBox(0,"__FileOpen()","$path = " & $path)
EndFunc

Func _Quit()
    Exit
EndFunc

Result (ignoring the syntax checker error to run it):

+>08:11:35 Starting AutoIt3Wrapper v.1.9.4
>Running AU3Check (1.54.10.0)  from:C:\Program Files\AutoIt3
C:\Program Files\AutoIt3\Scripts\Test_1.au3(11,47) : ERROR: __FileOpen($tf)(): undefined function.
        GUICtrlSetOnEvent(-1,"__FileOpen($tf)")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Scripts\Test_1.au3 - 1 error(s), 0 warning(s)
!>08:11:35 AU3Check ended.rc:2
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\AutoIt3\Scripts\Test_1.au3"  
C:\Program Files\AutoIt3\Scripts\Test_1.au3 (11) : ==> Unknown function name.: 
GUICtrlSetOnEvent(-1,"__FileOpen($tf)") 

->08:11:38 AutoIT3.exe ended.rc:1
+>08:11:38 AutoIt3Wrapper Finished

Edit: Tweaked demo to run the bad case (file ends in .url).

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...