LoWang Posted October 13, 2011 Share Posted October 13, 2011 #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> loadhosts() ;this works and a file browse dialog is displayed ;#cs Opt("GUIOnEventMode",1) #Region ### START Koda GUI section ### Form=Form1.kxf $Form1_1 = GUICreate("test", 605, 801, 202, 120) $Button2 = GUICtrlCreateButton("load hosts", 312, 16, 75, 25, $WS_GROUP) GUICtrlSetOnEvent(-1, "loadhosts") ;this does not work! GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $jed=1 While $jed Sleep(20) wend ;#ce Func loadhosts($filename="") if not $filename then $filename=FileOpenDialog("vyber soubor s IP nebo hostnamy",@scriptdir,"(*.*)",1) endif ;filereading commands here... endfunc Run this and you will see a file browse dialog - the function works when called directly. Then click a button to call the same function - and you will get an error saying that variable is not declared! WTH? If I add a statement local $filename in the function body then it says ERROR: $filename already declared as parameter, but obviously the declaration is being ignored when the function is called via a GUI control. I find this behaviour strange. Somebody please explain. I just want to be able to use this kind of function to repeatedly load files, but I don't want $filename to be global, because then I would have to reset it to "" everytime the function runs so it opens the browse dialog. Link to comment Share on other sites More sharing options...
KaFu Posted October 13, 2011 Share Posted October 13, 2011 As there is no functionallity to pass parameters to a function with GUICtrlSetOnEvent() or GUISetOnEvent(), you may only call functions not expecting any parameters. Here's a work-around: #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> loadhosts() ;this works and a file browse dialog is displayed ;#cs Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=Form1.kxf $Form1_1 = GUICreate("test", 605, 801, 202, 120) $Button2 = GUICtrlCreateButton("load hosts", 312, 16, 75, 25, $WS_GROUP) GUICtrlSetOnEvent(-1, "loadhosts_wrapper") ;this does not work! GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $jed = 1 While $jed Sleep(20) WEnd ;#ce Func loadhosts_wrapper() loadhosts() EndFunc Func loadhosts($filename = "") If Not $filename Then $filename = FileOpenDialog("vyber soubor s IP nebo hostnamy", @ScriptDir, "(*.*)", 1) EndIf ;filereading commands here... EndFunc ;==>loadhosts OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Mat Posted October 13, 2011 Share Posted October 13, 2011 Alternatively use IsDeclared to see if the function was called and parameter set or not: Func loadhosts($filename="") if not IsDeclared("filename") then $filename = "" if not $filename then $filename=FileOpenDialog("vyber soubor s IP nebo hostnamy",@scriptdir,"(*.*)",1) endif ;filereading commands here... endfunc Just be warned that obfuscater breaks this. LoWang 1 AutoIt Project Listing Link to comment Share on other sites More sharing options...
LoWang Posted February 1, 2012 Author Share Posted February 1, 2012 (edited) Another not very nice surprise: this problem occurs when I call such function via AdlibRegister also. So it is not only a GUI issue. I think this should rather be mentioned in the helpfile... Edited February 1, 2012 by LoWang Link to comment Share on other sites More sharing options...
jchd Posted February 2, 2012 Share Posted February 2, 2012 This is very clearly mentionned in the help file under the two functions you cite: You can not call a function using parameters. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
LoWang Posted February 2, 2012 Author Share Posted February 2, 2012 (edited) really? Not in my helpfile. Maybe it was added in autoit 3.3.8.0 which I did not install yet...It is not mentioned on the page about GUICtrlSetOnEvent either, so what do you mean please? Edited February 2, 2012 by LoWang Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 2, 2012 Moderators Share Posted February 2, 2012 LoWang,"You can not call a function using parameters" is indeed now mentioned on the v3.3.8.0 Help file pages for AdlibRegister, GUISetOnEvent and GUICtrlSetOnEvent. Remember that the Help file, just like AutoIt itself, is a living thing that constantly changes - best to update when a new version comes out. And there has never been a "parameters" parameter (if you see what I mean) in any of those commands - so there has never been any indication that you could use parameters with function to be called by them. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
LoWang Posted February 2, 2012 Author Share Posted February 2, 2012 (edited) aha so my suspicion was true. So I will upgrade. But it should have been specifically mentioned a long time ago IMO ;-) You know I use implicit parameters quite often and the obvious reason is to be able to call my funcs in certain parts of the code without parameters One might think that since AdlibRegister does not take parameters then it would work. But it is the same problem as with GuictrlSetonEvent... When you first see it, you may try to fix it by adding "local $var" declaration in the func body, but this way you get an error saying that $var is already declared as an implicit parameter which can be confusing, because it says exactly the opposite when you try to run the program and call the func by Adlib. I just think it is really not that clear for everybody so I wanted some more comments to it and I am glad it is in the help file now. Thanks Edited February 2, 2012 by LoWang Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now