增量变量定义

发布于 2024-12-05 08:44:58 字数 371 浏览 0 评论 0原文

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

所以不是这个:

$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

有谁知道怎么做?

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

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 variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-12-12 08:44:58

您正在寻找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。您应该做的是将这些值存储在数组中。

You're looking for Assign():

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 used too. What you should have done was storing those values in an array.

别闹i 2024-12-12 08:44:58

所以代替这个:

$Var1 = '这是变量一:P'
$Var2 = '这是变量二:P'

我想要这个(伪代码):

对于 $i = 1 到 UBound($People)-1   
    **$var[$i]** = GUICtrlCreateCheckbox($var[$i], 24, $y, 200, 17) 
    $y = $y + 25 
下一个

有人知道如何做到这一点吗?

根据文档 - 简介 - 数组

数组是包含一系列数据元素的变量。该变量中的每个元素都可以通过与数组中元素的位置相关的索引号来访问 - 在 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

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.).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文