Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 自动生成字段变量_Powershell_User Interface_Wmi - Fatal编程技术网

Powershell 自动生成字段变量

Powershell 自动生成字段变量,powershell,user-interface,wmi,Powershell,User Interface,Wmi,我使用以下代码在powershell GUI上生成复选框- $databases_checkBox_1 = New-Object system.windows.Forms.CheckBox $databases_checkbox_1.Width = 200 $databases_checkBox_1.location = new-object system.drawing.point(245,$y) $y=$y+30 $databases_checkBox_2 = New-Object sys

我使用以下代码在powershell GUI上生成复选框-

$databases_checkBox_1 = New-Object system.windows.Forms.CheckBox
$databases_checkbox_1.Width = 200
$databases_checkBox_1.location = new-object system.drawing.point(245,$y)
$y=$y+30

$databases_checkBox_2 = New-Object system.windows.Forms.CheckBox
$databases_checkbox_2.Width = 200
$databases_checkBox_2.location = new-object system.drawing.point(245,$y)
$y=$y+30

$databases_checkBox_3 = New-Object system.windows.Forms.CheckBox
$databases_checkbox_3.Width = 200
$databases_checkBox_3.location = new-object system.drawing.point(245,$y)
$y=$y+30

$databases_checkBox_4 = New-Object system.windows.Forms.CheckBox
$databases_checkbox_4.Width = 200
$databases_checkBox_4.location = new-object system.drawing.point(245,$y)
$y=$y+30

但我想生成“X”数量的复选框,因为列表可能多达100个-知道如何让它们自动生成吗?

不是最漂亮的解决方案,但应该可以。本例中仅创建5个变量0..4

$y = 0
0..4 | foreach {
    $currentVar = New-Variable -Name databases_checkBox_$_ -Value $(New-Object System.Windows.Forms.CheckBox) -PassThru
    $currentVar.Value.Width = 200
    $currentVar.Value.Location = New-Object System.Drawing.Point(245,$y)
    $y = $y+30
}
您可以通过运行Get Variable databases\u checkBox*进行双重检查


话虽如此,如果你要追上几百个,很快就会弄得一团糟。将Ansgar的评论作为一句忠告。

在循环中运行复选框创建代码,次数与要创建的复选框数量相同。不要忘记在创建控件后将其添加到表单中。在每次迭代结束时,输出创建的对象,并在变量中收集循环的所有输出:

$checkboxes = 0..3 | ForEach-Object {
    $cb = New-Object Windows.Forms.CheckBox
    $cb.Width = 200
    $cb.Location = New-Object Drawing.Point(245, $y)
    $y += 30

    $form.Controls.Add($cb)

    $cb
}
一旦你有了它,你就可以通过它在数组中的索引来访问每个复选框。例如:

$checkboxes[2].Checked

将为您提供第三个复选框的选中状态。PowerShell阵列是基于零的。

不要将单个变量用于此类情况。改用数组。你知道如何改变变量名吗?仅供参考:我需要能够参考不同的支票箱。一数组。是的,我明白了,我在问如何使用数组来实现这一点