Jump to content
Sign in to follow this  
Markus21

Create Functions in a Loop

Recommended Posts

Markus21

Hi, is it possible to create functions in a loop? e.g.:

for $i = 0 To 10 Step +1
    Func $i()
        MsgBox(0, "Name", "I am function" & $i)
    EndFunc
Next

Thank you!

Share this post


Link to post
Share on other sites
Airwolf

Hi, is it possible to create functions in a loop? e.g.:

for $i = 0 To 10 Step +1
    Func $i()
        MsgBox(0, "Name", "I am function" & $i)
    EndFunc
Next

Thank you!

Sorry to be blunt... but no. What is your purpose? There is probably a better way to do it.

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites
Markus21

I want to build a "dynamic" contextmenu. On execution my program reads data out of a database and should create a menuitem for every "piece of data" in the contextmenu, which is connected to a function (GUI event mode on).

Share this post


Link to post
Share on other sites
jokke

well then just do it the other way around, basicly what you are asking for.


UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.

Share this post


Link to post
Share on other sites
jokke

for $i = 0 To 10 Step +1
    somefunc($i)
Next

Func somefunc($i)
    MsgBox(0, "Name", "I am function" & $i)
EndFunc

Hehe know what you mean btw. Search the forum, you can use some execute or something like that, then you can execute code not depending on a set environment variable.

Edited by jokke

UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.

Share this post


Link to post
Share on other sites
PsaltyDS

I want to build a "dynamic" contextmenu. On execution my program reads data out of a database and should create a menuitem for every "piece of data" in the contextmenu, which is connected to a function (GUI event mode on).

I have created lots of GUIs with dynamic controls and I prefer GuiOnEventMode for all my GUIs.

The way you handle that it to have a single function that handles whole classes of control actions, like _ButtonHit() for all your buttons. Inside that function, you make use of @GUI_WINHANDLE, @GUI_CTRLID, etc. to detect which control to act on.

I think you'll find that much more logical and easier to code than "dynamic function declarations".

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
monoceres

To get you started:

#include <File.au3>
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)  
HotKeySet("{ESC}","close")
Global $array
_FileReadToArray(@ScriptDir&"\test.txt",$array)

Global $ids[UBound($array)]
For $i=1 To UBound($array)-1
    $ids[$i]=TrayCreateItem($array[$i])

    TrayItemSetOnEvent(-1,"_Handler")
Next

Do
    Sleep(100)
Until False


Func _Handler()
    For $i=0 To Ubound($array)-1
        If $ids[$i]=@TRAY_ID Then
            MsgBox(0,"Clicked",TrayItemGetText($ids[$i]))
            ExitLoop
        EndIf
    Next
EndFunc

Func close()
    Exit
EndFunc

Creates a tray item for every line in a text file ;)


Broken link? PM me and I'll send you the file!

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
Sign in to follow this  

×