Sign in to follow this  
Followers 0
Andrew Sparkes

Functions ...

7 posts in this topic

#1 ·  Posted (edited)

Is there any way to make functions sequentially with a loop? like func1, func2, func3 etc? or is it possible to pass parameters with a GUICtrlSetOnEvent()? I'm trying to read an ini, display buttons based on the ini contents and when you click a button, it does a function prescribed in the ini. Is this possible? or are the functions driving me legally insane? any insight? the passing parameter option is definately preferred.

this is nonworking code, but demonstrates what I want:

For $x to 5
    $t="test"+$x
    Func $t()
        MsgBox(0,"Test","This is a test")
    EndFunc
Next

Could I run the SetOnEvent like this?

$x="title"
$z="message"
GUICtrlCreateButton("Test Button",10,10,100,100)
GUICtrlSetOnEvent(-1,"testfunc")

Func testfunc()
otherfunc($x,$z)
Endfunc

Func otherfunc($x,$z)
MsgBox(0,$x,$z)
EndFunc

to pass parameters, as long as they were global, would that work?

Edited by Andrew Sparkes

---Sparkes.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

do you mean something like this?

While 1
t1()
t2()
t3()
t4()
Wend

Func t1()
        MsgBox(0,"Test1","This is a test",1)
EndFunc

Func t2()
        MsgBox(0,"Test2","This is a test",1)
EndFunc

Func t3()
        MsgBox(0,"Test3","This is a test",1)
EndFunc

Func t4()
        Exit
EndFunc

or am i missing the point of your question?

Edited by random667

It is really sad to see a family torn apart by something as simple as a pack of wolves.

Share this post


Link to post
Share on other sites

He's asking if you can dynamically create functions.

Here's my two cents -- using a good software design there should never be a need to be dynamic function creation. I understand where you are coming from, but there is always another solution. Consider making something similar to an Object (some functions in an .au3 that act as an Object).

Autoit does allow dynamic function calls (look at the Call( "function" ) B) )

Post some code/give more details if you need more

Hope that helps! :o

aarnott

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That function can be used with what I would like to do, but I'll explain further.

I currently have a script that will read an ini, and make buttons with a key from the ini as names. I want the button to pass the key and additional info from the ini, depending on which button. It is a autologin script with multiple accounts. (Imagine the below are buttons)

User1

User2

When the user clicks the button for user1, for example, it reads the email address and password for the section "User1" and then passes that information to a login function.

I need the OnEvent of the buttons to pass the location of the info.

here are some snippets:

actual script:

$ini="accounts.ini"
Dim $sn
$sn = IniReadSectionNames($ini)
GUICreate("Logger-Inner",300,$sn[0]*30,(@DesktopWidth/2-150),(@DesktopHeight/2-$sn[0]*15))
For $x=1 to $sn[0]
$email=IniRead($ini,$sn[$x],"email","Error")
GUICtrlCreateButton($email,100,30,0,($x-1)*30)
Next
GUISetState()

all that does is make the gui. I want the buttons to use the info from the ini to login.

sample ini:

[User1]
email=User1@Domain.com
password=User1Password

In html forms, it would simply be

<Input type="button" onclick="login(username,password)" name="Login">

how can I do that in Autoit? passing username and password to a function that is.

Edited by Andrew Sparkes

---Sparkes.

Share this post


Link to post
Share on other sites

In html forms, it would simply be

<Input type="button" onclick="login(username,password)" name="Login">

how can I do that in Autoit? passing username and password to a function that is.

This is kind of a kludge, but based on your existing code, you could try this:

#include <GuiConstants.au3>
 
 $ini = "accounts.ini"
 Dim $sn
 $sn = IniReadSectionNames($ini)
 GUICreate("Logger-Inner", 300, $sn[0] * 30, (@DesktopWidth / 2 - 150) , (@DesktopHeight / 2 - $sn[0] * 15))
 Dim $Buttons[$sn[0]]
 
 For $x = 0 To $sn[0] -1
     $email = IniRead($ini, $sn[$x + 1], "email", "Error")
     $Buttons[$x] = GUICtrlCreateButton($email, 0, $x * 30, 100, 30)
 Next
 GUISetState()
 
 
 While 1
     $msg = GUIGetMsg()
     Select
     Case $msg = $GUI_EVENT_CLOSE
             ExitLoop
         Case Else
             For $i = 0 To UBound($Buttons) -1 
                 If $msg = $Buttons[$i] Then
                     $UserName = IniRead($ini, $sn[$i + 1], "email", "Error")
                     $Password = IniRead($ini, $sn[$i + 1], "password", "Error")
                     Login ($UserName, $Password)
                 EndIf
             Next
     EndSelect
 WEnd
 Exit
 
 Func Login ($UN, $PW)
    ; Function code goes here
     MsgBox(0, "Login Details", "User: " & $UN & @LF & "Password: " & $PW)
 EndFunc  ;==>Login

You could improve on this by storing the users in an array (so as to avoid re-reading the INI).

You could also make the button width variable based on the length of the longest value.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

Your code is flawless and works like a charm, but I still have a few questions.

One, how does this work for this script:

$Buttons[$x] = GUICtrlCreateButton($email, 0, $x * 30, 100, 30)

I have tried for looping and assigning to $array[$x] but kept giving "Expected a "=" operator in assignment statement.:"

I don't understand why it works in that situation, but not in this...

Another, you mentioned reading the accounts into an array. Just off the spar of the moment, I tried to code that, and failed miserably. This code below is wrong, but I'd like to know how to fix it.

Dim $accarray
For $x=1 To UBound($sn)
$accarray[$x]=IniReadSection($ini,$sn[$x])
Next
For $t=1 To UBound($accarray)
    MsgBox(4096,"","section: "&$sn[$t]&@CRLF&"key: "&$accarray[$t][1][0]&@CRLF&"value: "&$accarray[$t][1][1])
Next

I am a new coder, so please forgive the newb mistakes in the code.

For finding the length of the longest value, is there a builtin function for counting characters? or is it in a UDF library elsewhere?

Thanks for the help. I appreciate it.


---Sparkes.

Share this post


Link to post
Share on other sites

I have tried for looping and assigning to $array[$x] but kept giving "Expected a "=" operator in assignment statement.:"

I don't understand why it works in that situation, but not in this...

If it applies to the code you just posted, see below.

If not, post the code here or PM it to me.

This code below is wrong, but I'd like to know how to fix it.

Dim $accarray
For $x=1 To UBound($sn)
$accarray[$x]=IniReadSection($ini,$sn[$x])
Next
For $t=1 To UBound($accarray)
    MsgBox(4096,"","section: "&$sn[$t]&@CRLF&"key: "&$accarray[$t][1][0]&@CRLF&"value: "&$accarray[$t][1][1])
Next
Here, you're using $accarray as an array, but it hasn't been declared as one.

Look at how I declared the $Buttons array in my previous post.

However, there are two reasons I would not use an array for this:

  • $accarray ^should^ be an array, and I'm not sure how INIReadSection() would handle this. (Will it make it a 3-dimensional array?)
  • If the field order is altered in the INI file, your script would have to handle it.

For finding the length of the longest value, is there a builtin function for counting characters? or is it in a UDF library elsewhere?

Nothing built in, AFAIK.

The closest thing to it would be the _ArrayMax() function.

Try this:

Func ArrayMaxLength($Array)
      Local $MaxLen = 0, $Curlen, $x
      
      If Not IsArray($Array) Then
          Return StringLen($Array)
      EndIf
      
      For $x = 0 To UBound($Array)
          $Curlen = StringLen($Array[$x])
          If $Curlen > $MaxLen Then $MaxLen = $Curlen
      Next
      Return $MaxLen
  EndFunc  ;==>ArrayMaxLength

Once you get the length of the longest string, you'll want to multiply that by the width of the widest character ("W") in whichever font you're using.

Let me know if you get stuck.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

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  
Followers 0