topten

Dynamic function creation, how?

9 posts in this topic

I have found an example of dynamic creating buttons and modified it to my needs

 

#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 option

GUICtrlSetOnEvent($Button[$i], "somefunc")

Share this post


Link to post
Share on other sites



This works assuming that ids are in sequential order.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

This works assuming that ids are in sequential order.

Could you please give a short example?

@JohnOne

Say  I have

$Button[$i] = GUICtrlCreateButton("Button"&$i, 16, $y, 75, 25)

and then I assign function to the button of $i

GUICtrlSetOnEvent($Button[$i], "somefunc" & $i)

How can I declare such function later on? something like

func somefunc&$i()

msgbox (0, "", "the button" & $i & "was pressed")

endfunc

 

Share this post


Link to post
Share on other sites

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.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

topten,

Two examples...

Not using array to store CTL id's or function names

#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
endfunc

Using an array to store the CTL id and function name (common usage)

#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
endfunc

kylomas

Edited 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

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