Jump to content

Incrementing variable names in a for loop


Recommended Posts

Apologies if this has been covered before.

I'm trying to build a gui in which I'm going to create several buttons.  The number of buttons will vary based on how many entries I put into an ini file.  I'm using this to issue to commands to another window and sometimes it'll be 15 commands, sometimes 50.  I'd like the script to look at how big my array is, and I'll do a "for $i = 0 to Ubound($commands)"  I'd like to simply name the buttons :

$button1 = guictrlcreate(.....

$button2 = guictrlcreate(.....

$button3 = guictrlcreate(.....

What I can't quite figure out is how can I create these variables on the fly with this naming convention within the for loop

I was trying $button & $i, but that obviously doesn't work.  Any tips would be great appreciated.

 

Thanks!

 

Link to comment
Share on other sites

2 hours ago, rizzlebizznizzle said:

Any tips would be great appreciated.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Form()
Func Form()

    ; read the INI, get the info. into an array
    Local $nMsg, $n, $aArray[5][3]
    $aArray[1][0] = "data from INI # 1"
    $aArray[2][0] = "data from INI # 2"
    $aArray[3][0] = "data from INI # 3"
    $aArray[4][0] = "data from INI # 4"
    $aArray[0][0] = UBound($aArray) - 1
    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Form made with Koda", 300, $aArray[0][0] * 25 + 40, 192, 124)

    For $n = 0 To $aArray[0][0] - 1 ; modify to something like this
        $aArray[$n + 1][1] = GUICtrlCreateButton($aArray[$n + 1][0], 24, 16 + ($n * 25), 150, 25)
    Next

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case 0, -7, -8, -9, -10, -11 ; or remove these and play with your mouse on the form
                ; nothing, to save CPU
                
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                ExitLoop
                
            Case Else
                ConsoleWrite('-> $nMsg = ' & $nMsg & @CRLF)
                For $n = 1 To $aArray[0][0]
                    If $aArray[$n][1] = $nMsg Then ConsoleWrite('+> $nMsg matched button "' & $aArray[$n][0] & '"' & @CRLF)
                Next

        EndSwitch
    WEnd
EndFunc   ;==>Form

:) 

Spoiler
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Form()
Func Form()

    ; read the INI, get the info. into an array

    Local $xManyButtons = 10

    Local $nMsg, $n, $aArray[$xManyButtons + 1][2]
    For $n = 1 To UBound($aArray) - 1
        $aArray[$n][0] = "data from INI # " & $n
    Next
    $aArray[0][0] = UBound($aArray) - 1

    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Form made with Koda", 300, $aArray[0][0] * 25 + 32, 192, 124)

    For $n = 0 To $aArray[0][0] - 1 ; modify to something like this
        $aArray[$n + 1][1] = GUICtrlCreateButton($aArray[$n + 1][0], 24, 16 + ($n * 25), 150, 25)
    Next

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case 0, -7, -8, -9, -10, -11 ; or remove these and play with your mouse on the form
                ; nothing, to save CPU

            Case $GUI_EVENT_CLOSE
                GUIDelete()
                ExitLoop

            Case Else
                ConsoleWrite('-> $nMsg = ' & $nMsg & @CRLF)
                For $n = 1 To $aArray[0][0]
                    If $aArray[$n][1] = $nMsg Then ConsoleWrite('+> $nMsg matched button "' & $aArray[$n][0] & '"' & @CRLF)
                Next

        EndSwitch
    WEnd
EndFunc   ;==>Form

 

 

Edited by argumentum
=)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...