【问题标题】:Incremental variable definition增量变量定义
【发布时间】:2011-11-20 21:00:37
【问题描述】:

我想自动定义递增的变量名。

所以不要这样:

$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

有人知道怎么做吗?

代码应该创建与数组中定义的一样多的复选框,并且每个复选框都应该有自己的变量。

【问题讨论】:

    标签: arrays variables checkbox autoit


    【解决方案1】:

    所以不要这样:

    $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() 演示
    • Main() 演示单个元素的选择(使用 GUICtrlSetData() 更改其文本)。
    • 根据需要调整常量($g_iBoxAmount 等)。

    【讨论】:

      【解决方案2】:

      您正在寻找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)
      

      显然,var2var3 也可以使用 :)

      编辑:为了清楚起见,如果你做得正确,你会做的就是将这些值存储在一个数组中——这是处理这类事情的最佳方法。 p>

      【讨论】:

      • @DemonWareXT "你正在寻找 Assign 函数!" 这不是应该避免的吗("所以而不是这个 ")?
      猜你喜欢
      • 2015-10-17
      • 2016-11-26
      • 2012-03-19
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      相关资源
      最近更新 更多