开发者

Incremental variable definition

I want to automatically define incrementing variable names.

So instead of this:

$Var1 = 'This is variable one :P'
$Var2 = 'This is variable two :P'

I'd like this (pseudo code):

For $i = 1 to UBound($People)-1     
    **$var[$i]** = GUICtrlCreateCheckbox($var[$i], 24, $y, 200, 17) 
    $y = $y + 25 
Next

Does anyone know how?

The code should make as many checkboxes as defined in an array and every checkbox should have its own v开发者_开发问答ariable.


You're looking for the Assign function!

Check out this example:

For $i = 1 To 5
    Assign('var' & $i, $i);
Next

Then you can access these variables with:

MsgBox(4096, "My dynamic variables", $var1)
MsgBox(4096, "My dynamic variables", $var3)
MsgBox(4096, "My dynamic variables", $var5)

Obviously, var2 and var3 can be utilised too :)

Edit: For clarity, what you would have been doing, if you had done it properly, was storing those values in an array - which is the best method for this kind of thing.


so instead of this:

$Var1 = 'This is variable one :P'
$Var2 = 'This is variable two :P'

I'd like this (pseudo code):

For $i = 1 to UBound($People)-1   
    **$var[$i]** = GUICtrlCreateCheckbox($var[$i], 24, $y, 200, 17) 
    $y = $y + 25 
Next

Does anyone know how this could be done?

As per Documentation - Intro - Arrays:

An Array is a variable containing a series of data elements. Each element in this variable can be accessed by an index number which relates to the position of the element within the Array - in AutoIt the first element of an Array is always element [0]. Arrays elements are stored in a defined order and can be sorted.

Dynamic checkbox creation example, assigning multiple checkbox-control identifiers to a single array (untested, no error-checking):

#include <GUIConstantsEx.au3>

Global Const $g_iBoxStartX = 25
Global Const $g_iBoxStartY = 25
Global Const $g_iBoxWidth  = 100
Global Const $g_iBoxHeight = 15
Global Const $g_iBoxSpace  = 0
Global Const $g_iBoxAmount = Random(2, 20, 1)
Global Const $g_iBoxTextEx = $g_iBoxAmount -1
Global Const $g_sBoxTextEx = 'Your text here.'

Global Const $g_iWindDelay = 50
Global Const $g_iWinWidth  = $g_iBoxWidth * 2
Global Const $g_iWinHeight = ($g_iBoxStartY * 2) + ($g_iBoxHeight * $g_iBoxAmount) + ($g_iBoxSpace * $g_iBoxAmount)
Global Const $g_sWinTitle  = 'Example'

Global       $g_hGUI
Global       $g_aID

Main()

Func Main()

    $g_hGUI = GUICreate($g_sWinTitle, $g_iWinWidth, $g_iWinHeight)
    $g_aID  = GUICtrlCreateCheckboxMulti($g_iBoxStartX, $g_iBoxStartY, $g_iBoxWidth, $g_iBoxHeight, $g_iBoxSpace, $g_iBoxAmount)

    GUICtrlSetData($g_aID[$g_iBoxTextEx], $g_sBoxTextEx)
    GUISetState(@SW_SHOW, $g_hGUI)

    While Not (GUIGetMsg() = $GUI_EVENT_CLOSE)

        Sleep($g_iWindDelay)

    WEnd

    Exit
EndFunc

Func GUICtrlCreateCheckboxMulti(Const $iStartX, Const $iStartY, Const $iWidth, Const $iHeight, Const $iSpace, Const $iAmount, Const $sTextTpl = 'Checkbox #%s')
    Local $iYPosCur = 0
    Local $sTextCur = ''
    Local $aID[$iAmount]

    For $i1 = 0 To $iAmount -1

        $iYPosCur = $iStartY + ($iHeight * $i1) + ($iSpace * $i1)
        $sTextCur = StringFormat($sTextTpl, $i1 +1)
        $aID[$i1] = GUICtrlCreateCheckbox($sTextCur, $iStartX, $iYPosCur, $iWidth, $iHeight)

    Next

    Return $aID
EndFunc
  • GUICtrlCreateCheckboxMulti() demonstrates
    • array declaration ($aID[$iAmount])
    • assignment of array elements in a For...To...Step...Next -loop ($aID[$i1] = ...).
  • Main() demonstrates selection of an individual element (changing its text using GUICtrlSetData()).
  • Adjust constants as required ($g_iBoxAmount etc.).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜