增量变量定义
问题描述:
我想自动定义增量变量名称。增量变量定义
因此,不是这样的:
$Var1 = 'This is variable one :P'
$Var2 = 'This is variable two :P'
我想这(伪代码):
For $i = 1 to UBound($People)-1
**$var[$i]** = GUICtrlCreateCheckbox($var[$i], 24, $y, 200, 17)
$y = $y + 25
Next
有谁知道怎么样?
代码应该创建数组中定义的多个复选框,每个复选框应该有自己的变量。
答
您在寻找Assign
的功能!
看看这个例子:
For $i = 1 To 5
Assign('var' & $i, $i);
Next
然后,您可以用访问这些变量:
MsgBox(4096, "My dynamic variables", $var1)
MsgBox(4096, "My dynamic variables", $var3)
MsgBox(4096, "My dynamic variables", $var5)
显然,var2
和var3
也得以利用:)
编辑:为了清楚起见,如果你做得很好,你会怎么做,是否存储这些val在数组中 - 这是这类事情的最佳方法。
答
因此,不是这样的:
$Var1 = 'This is variable one :P' $Var2 = 'This is variable two :P'
我想这(伪代码):
For $i = 1 to UBound($People)-1 **$var[$i]** = GUICtrlCreateCheckbox($var[$i], 24, $y, 200, 17) $y = $y + 25 Next
有谁知道怎么可以这样做?
作为每Documentation - Intro - Arrays:
数组是包含一系列数据元素的变量。此变量中的每个元素都可以通过索引号访问,该索引号与数组内的元素位置相关 - 在AutoIt中,数组的第一个元素始终为元素[0]。数组元素按照定义的顺序存储并可以排序。
动态复选框创建例如,分配多个checkbox-control标识符单个阵列(未经测试,没有错误检查):
#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()
演示- 数组声明(
$aID[$iAmount]
) - 指定
For...To...Step...Next
中的数组元素 - 回路($aID[$i1] = ...
)。
- 数组声明(
-
Main()
演示选择单个元素(使用GUICtrlSetData()
更改其文本)。 - 根据需要调整常量(
$g_iBoxAmount
等)。
@DemonWareXT“_您正在寻找'Assign'功能!_”是不是要避免(“_so而不是this_”)? – user4157124