Jump to content
Sign in to follow this  
thepip3r

Question about my custom function OR a problem with understanding GUICtrlSetOnEvent

Recommended Posts

thepip3r

I'm using FindFileFirstFile and FindFileNextFile in a function I wrote to take a given directory and do a recursive search on every file and folder underneath of the passed path and builds a hierarchical menu based off of that. The function works exactly how I want it to except the button on click errors out and I'm not sure if it's because of an incorrect usage of the GUICtrlSetOnEvent or because of an error with my functions. Could anyone please take a look and offer suggestions?? I'd greatly appreciate it.

Function to build a hierarchical menu based off of a given path:

__DirtoMenu("C:\Documents and Settings\All Users\Start Menu\Programs",$menuAllProgsParent); $menuAllProgsParent is the var for the parent menu I want the tree to start from.

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
        
        $fileAttrib = FileGetAttrib($fp & "\" & $file)
        If @error Then 
            GUICtrlCreateMenuItem("<Parsing Error>",$menuTempParent)
            ExitLoop
        Else
            If StringInStr($fileAttrib, "D") Then
                $newMenu = GUICtrlCreateMenu($file,$menu)
                __DirtoMenu($fp & "\" & $file,$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,"__ExePath"); <---- Problem Section !!!!!!!!!!!!!!!!
                    Else
                        GUICtrlCreateMenuItem($file,$menu)
                        GUICtrlSetOnEvent(-1,"__ExePath"); <---- Problem Section !!!!!!!!!!!!!!!!
                    EndIf
                EndIf
            EndIf
        EndIf
    WEnd

    FileClose($search)
EndFunc

Here are the custom functions I wrote to try and get the menu on-clicks to try and execute the path:

Func __ExePath()
    ShellExecute(GUICtrlRead(@GUI_CtrlId,1))
EndFunc

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

When I click on the buttons generated by the function as it is written above, I get an error citing the ShellExecute(GUICtrlRead(@GUI_CtrlId,1)) stating: Error: Unable to execute the external program. The system cannot find the file specified.

And when I replace __ExePath with __FileOpen($file) in my __DirtoMenu() function, my script errors on execution citing an unknown function on line 225 -- the line showing the first ; <---- Problem Section !!!!!!!!!!!!!!!!

Any help would be greatly greatly appreciated... TIA.


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

Share this post


Link to post
Share on other sites
Siao

The text of your menu items doesn't have file extension let alone full path, so what exactly do you expect ShellExecute to do with it, unless by some lucky chance a file like that does exist somewhere on system path and can be executed (which wouldn't be what you intended anyway)?

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites
thepip3r

Siao -- you're spot on. I actually came upon that conclusion almost immediately after my post and I'm sorry for not making an update before you posted. I figured it out by changing my function to this:

Func __ExePath()
    MsgBox(0,"",GUICtrlRead(@GUI_CtrlId,1))
    ShellExecute(GUICtrlRead(@GUI_CtrlId,1))
EndFunc

...So then I guess my question changes to how do I make the button to say the executable's/link's/URL's name but when clicked, passes the path? Is there a way to store multiple values in a button?? Because in my function, I do have the path to the file, I just can't pass it the same way or my whole button would be named the absolute path to the file...

My ultimate intention is to (as in the example) build the menu of the All Users/Start Menu/Programs directory, create submenus when the function encounters another directory, and when finally when it gets to a file inside of the directory, it writes the name of the file to the menu but executes the path to the file in whatever program or way the systems it's being run from knows how to run that particular file...

any further help?

Edited by thepip3r

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

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  

×