Jump to content

implicit variable value does not work when func is called via GUI??


LoWang
 Share

Recommended Posts

#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

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 3 months later...

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 by LoWang
Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

really? Not in my helpfile. Maybe it was added in autoit 3.3.8.0 which I did not install yet...

Posted Image

It is not mentioned on the page about GUICtrlSetOnEvent either, so what do you mean please?

Edited by LoWang
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by LoWang
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...