dRsrb Posted February 8, 2009 Posted February 8, 2009 Hi everybody! I'm new to autoit and I like it VERY much! But I have a problem, that I don't understand. I extract the following code segment out of my application: expandcollapse popup#include <GUIConstantsEx.au3> AutoItSetOption('GUIOnEventMode', 1) ;AutoItSetOption('MustDeclareVars', 1) Global Const $ApplicationTitle = 'Test' Global Const $DefaultTestDirectory = 'C:\' Global $Main Global $TestDirectoryInput, $CurrentTestDirectory _Main() Func _Main() ; GUI: Main window $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication') $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43) GUICtrlCreateLabel('Test', 17, 19, 38, 17) $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19) GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED) $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0) GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc') $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1) GUISetState(@SW_SHOW) ; _TestFunc() While True Sleep(1000) WEnd ; GUIDelete() EndFunc Func _ExitApplication() If @GUI_WINHANDLE = $Main Then Exit 0 EndIf EndFunc Func _TestFunc($sTestDirectory = $CurrentTestDirectory) MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory) MsgBox(0, '$sTestDirectory', $sTestDirectory) EndFunc The Problem is when I press the 'Test'-Button and call my 'TestFunc'-function I get an AutoIt Error - "Variable used without being declared". But the variable is declared in the signature of the function. Hmmm, so why does 'GUICtrlSetOnEvent' have another behaviour then calling 'TestFunc()' somewhere else? 2nd question: Do I need 'GUIDelete()' when 'AutoItSetOption('GUIOnEventMode', 1)' is? Bye dRsrb
FireFox Posted February 8, 2009 Posted February 8, 2009 @dRsrb expandcollapse popup#include <GUIConstantsEx.au3> AutoItSetOption('GUIOnEventMode', 1) ;AutoItSetOption('MustDeclareVars', 1) Global $ApplicationTitle = 'Test' Global $DefaultTestDirectory = 'C:\' Global $Main Global $TestDirectoryInput, $CurrentTestDirectory _Main() Func _Main() ; GUI: Main window $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication') $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43) GUICtrlCreateLabel('Test', 17, 19, 38, 17) $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19) GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED) $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0) GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc') $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1) GUISetState(@SW_SHOW) ; _TestFunc() While True Sleep(1000) WEnd ; GUIDelete() EndFunc ;==>_Main Func _ExitApplication() If @GUI_WinHandle = $Main Then Exit 0 EndIf EndFunc ;==>_ExitApplication Func _TestFunc() $sTestDirectory = $CurrentTestDirectory MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory) MsgBox(0, '$sTestDirectory', $sTestDirectory) EndFunc ;==>_TestFunc 2nd question: Do I need 'GUIDelete()' when 'AutoItSetOption('GUIOnEventMode', 1)' is?Look at GuiSetState and @SW_HIDE Cheers, FireFox.
Moderators Melba23 Posted February 8, 2009 Moderators Posted February 8, 2009 dRsrb,Answer to first question: You cannot send parameters when using the OnEvent mode - it is the main limitation. However, you might like to look here where martin has posted a UDF to get over this. I have not yet played with this - on my To-Do list somewhere!Answer to second question: When you Exit, all GUIs are deleted automatically, so your _ExitApplication() function will delete the GUI for you. This behaviour is independent of GUI mode. Where you have GUIDelete() will never be reached by your code anyway - but I am sure you realise that.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
KaFu Posted February 8, 2009 Posted February 8, 2009 Problem was, that you called the function with guionevent which doesn't permit function calling with params. expandcollapse popup#include <GUIConstantsEx.au3> AutoItSetOption('GUIOnEventMode', 1) ;AutoItSetOption('MustDeclareVars', 1) Global Const $ApplicationTitle = 'Test' Global Const $DefaultTestDirectory = 'C:\' Global $Main Global $TestDirectoryInput, $CurrentTestDirectory _Main() Func _Main() ; GUI: Main window $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication') $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43) GUICtrlCreateLabel('Test', 17, 19, 38, 17) $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19) GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED) $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0) GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc') GUISetState(@SW_SHOW) ; _TestFunc() While True Sleep(1000) WEnd ; GUIDelete() EndFunc Func _ExitApplication() If @GUI_WINHANDLE = $Main Then Exit 0 EndIf EndFunc Func _TestFunc() $sTestDirectory = GUICtrlRead($TestDirectoryInput) $CurrentTestDirectory = $sTestDirectory MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory) MsgBox(0, '$sTestDirectory', $sTestDirectory) EndFunc 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)
dRsrb Posted February 8, 2009 Author Posted February 8, 2009 (edited) Thank you very much guys! So I will need to call a intermediate function to declare and to finally call my '_TestFunc()'! expandcollapse popup#include <GUIConstantsEx.au3> AutoItSetOption('GUIOnEventMode', 1) ;AutoItSetOption('MustDeclareVars', 1) Global Const $ApplicationTitle = 'Test' Global Const $DefaultTestDirectory = 'C:\' Global $Main Global $TestDirectoryInput, $CurrentTestDirectory _Main() Func _Main() ; GUI: Main window $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication') $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43) GUICtrlCreateLabel('Test', 17, 19, 38, 17) $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19) GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED) $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0) GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc_Temp') $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1) GUISetState(@SW_SHOW) _TestFunc() While True Sleep(1000) WEnd EndFunc Func _ExitApplication() If @GUI_WINHANDLE = $Main Then Exit 0 EndIf EndFunc Func _TestFunc_Temp() $sTestDirectory = $CurrentTestDirectory _TestFunc(sTestDirectory) EndFunc Func _TestFunc($sTestDirectory = $CurrentTestDirectory) MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory) MsgBox(0, '$sTestDirectory', $sTestDirectory) EndFunc Great! Thank you! Bye dRsrb Edited February 8, 2009 by dRsrb
martin Posted February 8, 2009 Posted February 8, 2009 Easiest way is probably a Kafu suggests. If you need to have parameters passed then as Melba23 mentioned, you could use a udf to do it. Here is an example using my onevent udf (link in my signature) which assumes the udf has been saved in the same folder as your script. expandcollapse popup#include <GUIConstantsEx.au3> #include "onEventFunc.au3" AutoItSetOption('GUIOnEventMode', 1) ;AutoItSetOption('MustDeclareVars', 1) Global Const $ApplicationTitle = 'Test' Global Const $DefaultTestDirectory = 'C:\' Global $Main Global $TestDirectoryInput, $CurrentTestDirectory,$somedirectory = @ScriptDir _Main() Func _Main() ; GUI: Main window $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication') $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43) GUICtrlCreateLabel('Test', 17, 19, 38, 17) $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19) GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED) $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0) ;GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc',) SetOnEventA($TestDirectoryButton, '_TestFunc', $paramByRef,"$someDirectory") $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1) GUISetState(@SW_SHOW) ; _TestFunc() While True Sleep(1000) WEnd ; GUIDelete() EndFunc Func _ExitApplication() If @GUI_WINHANDLE = $Main Then Exit 0 EndIf EndFunc Func _TestFunc($sTestDirectory = $CurrentTestDirectory) MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory) MsgBox(0, '$sTestDirectory', $sTestDirectory) EndFunc Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
dRsrb Posted February 8, 2009 Author Posted February 8, 2009 Thank you martin too! Your UDF sounds interesting. I will definitely check it! Bye dRsrb
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