topten Posted August 4, 2015 Share Posted August 4, 2015 I have found an example of dynamic creating buttons and modified it to my needs expandcollapse popup#include <GUIConstantsEx.au3> $t = 20 Global $Button[$t], $iValueToWorkWith $hGUI = GUICreate("Test", 205, 406, -1, -1) $iStart = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $y = 16 for $i = 0 to $t-1 $Button[$i] = GUICtrlCreateButton("Button"&$i, 16, $y, 75, 25) $y = $y+20 next $iEnd = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE ConsoleWrite($nMsg & @CRLF) Exit Case $iStart To $iEnd ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ConsoleWrite(GUICtrlRead($nMsg) & ' ' & 'was pressed' & @CRLF) $iValueToWorkWith = $nMsg - $iStart ; Adjust as required GUIDelete($hGUI) ExitLoop EndSwitch WEnd ConsoleWrite('The value to use in the rest of our script now is: ' & $iValueToWorkWith & @CRLF)How can I dynamically assign functions in case of using Onevent optionGUICtrlSetOnEvent($Button[$i], "somefunc") Link to comment Share on other sites More sharing options...
JohnOne Posted August 4, 2015 Share Posted August 4, 2015 There is no dynamic button creation, please explain. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
guinness Posted August 4, 2015 Share Posted August 4, 2015 This works assuming that ids are in sequential order. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
topten Posted August 4, 2015 Author Share Posted August 4, 2015 This works assuming that ids are in sequential order.Could you please give a short example?@JohnOneSay I have $Button[$i] = GUICtrlCreateButton("Button"&$i, 16, $y, 75, 25)and then I assign function to the button of $iGUICtrlSetOnEvent($Button[$i], "somefunc" & $i)How can I declare such function later on? something likefunc somefunc&$i()msgbox (0, "", "the button" & $i & "was pressed")endfunc Link to comment Share on other sites More sharing options...
guinness Posted August 4, 2015 Share Posted August 4, 2015 A short example of what? It's a little hard to demo, as it can be sometime sporadic. But in the SciTE Jump thread (Examples) I mentioned sometime similar. Perhaps look there. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
mikell Posted August 4, 2015 Share Posted August 4, 2015 GUICtrlSetOnEvent($Button[$i], "_somefunc") func _somefunc() For $i = 0 to 10 If @GUI_CtrlId = $Button[$i] Then msgbox (0, "", "the button" & $i & "was pressed") Next endfuncVery hazardous though Link to comment Share on other sites More sharing options...
topten Posted August 4, 2015 Author Share Posted August 4, 2015 @guinness I know I was not very clear with what I meant but@mikell You have got my idea- this is what I was talking about.Why is it so hazardous? May it happen that the function give a break at some point? Do you think that there is some more reliable way of doing that?Great thanx to all Link to comment Share on other sites More sharing options...
JohnOne Posted August 4, 2015 Share Posted August 4, 2015 For $i = 0 To $Whatever $Button[$i] = GUICtrlCreateButton("Button" & $i, 16, $y, 75, 25) GUICtrlSetOnEvent(-1, $Function[$i]) NextYou get the idea. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted August 4, 2015 Share Posted August 4, 2015 (edited) topten,Two examples...Not using array to store CTL id's or function namesexpandcollapse popup#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) local $iNumButtons = 5 local $gui010 = guicreate('',160,50) GUISetOnEvent ( $GUI_EVENT_CLOSE, "_exit" ) for $i = 0 to $iNumButtons guictrlcreatebutton($i+1, $i*30+10, 20, 20) guictrlsetonevent(-1,'Process_Button') next guisetstate() while 1 sleep(9999999) WEnd func Process_Button() call("button" & guictrlread(@gui_ctrlid)) ; call whatever btton is returned in $gui_ctlid EndFunc func Button1() ConsoleWrite('Button1' & @CRLF) endfunc func Button2() ConsoleWrite('Button2' & @CRLF) endfunc func Button3() ConsoleWrite('Button3' & @CRLF) endfunc func Button4() ConsoleWrite('Button4' & @CRLF) endfunc func Button5() ConsoleWrite('Button5' & @CRLF) endfunc func _exit() Exit endfuncUsing an array to store the CTL id and function name (common usage)expandcollapse popup#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) local $iNumButtons = 5 local $aButtons[$iNumButtons][2] local $gui010 = guicreate('',160,50) GUISetOnEvent ( $GUI_EVENT_CLOSE, "_exit" ) for $i = 0 to ubound($aButtons) - 1 $aButtons[$i][0] = guictrlcreatebutton($i+1, $i*30+10, 20, 20) ; store the ctlid $aButtons[$i][1] = 'Button' & $i+1 ; store the name of the function to run guictrlsetonevent(-1,$aButtons[$i][1]) ; set ctl event to the name just stored next guisetstate() while 1 sleep(9999999) WEnd func Button1() ConsoleWrite('Button1' & @CRLF) endfunc func Button2() ConsoleWrite('Button2' & @CRLF) endfunc func Button3() ConsoleWrite('Button3' & @CRLF) endfunc func Button4() ConsoleWrite('Button4' & @CRLF) endfunc func Button5() ConsoleWrite('Button5' & @CRLF) endfunc func _exit() Exit endfunckylomas Edited August 5, 2015 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill 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