我一直试图自己找到这个,但我不完全确定如何说出这个问题。基本上我在这里做的是获取一个名称数组,并从中创建一个每个名称的复选框。编辑:我认为下面的代码片段也有效,但看起来这可能实际上没有用?
New-object System.Windows.Forms.CheckBox | New-Variable -Name ("checkbox" + $_)
我遇到的主要问题是:如何在创建这些变量命名复选框后实际设置属性?基本上如何获取每次迭代的变量名称,然后执行$ checkbox.margin和此类设置。我已经在下面发布了我的整个脚本,这是非常的WIP,我知道还有一些其他的东西很奇怪,但我打算在我解决这个问题后对其进行排序。
$form = New-Object System.Windows.Forms.Form
$flowlayoutpanel = New-Object System.Windows.Forms.FlowLayoutPanel
$buttonOK = New-Object System.Windows.Forms.Button
$usernames = "andrew", "beth", "charlie", "dave", "james", "george"
$totalvalues = ($usernames.count)
$formsize = 85 + (30 * $totalvalues)
$flowlayoutsize = 10 + (30 * $totalvalues)
$buttonplacement = 40 + (30 * $totalvalues)
$form_Load = {
0..$totalvalues|%{
New-object System.Windows.Forms.CheckBox | New-Variable -Name ("checkbox" + $_)
('$checkbox' + $_).Margin = '10, 8, 0, 0'
$checkbox.Margin = '10, 8, 0, 0'
$checkbox.Name = 'checkbox' + $_
$checkbox.Size = '200, 22'
$checkbox.Text = "" + $usernames[$_]
$checkbox.TextAlign = 'MiddleLeft'
$flowlayoutpanel.Controls.Add($checkbox)
}
}
$form.Controls.Add($flowlayoutpanel)
$form.Controls.Add($buttonOK)
$form.AcceptButton = $buttonOK
$form.AutoScaleDimensions = '8, 17'
$form.AutoScaleMode = 'Font'
$form.ClientSize = "500 , $formsize"
$form.FormBorderStyle = 'FixedDialog'
$form.Margin = '5, 5, 5, 5'
$form.MaximizeBox = $False
$form.MinimizeBox = $False
$form.Name = 'form1'
$form.StartPosition = 'CenterScreen'
$form.Text = 'Form'
$form.add_Load($form_Load)
$flowlayoutpanel.BorderStyle = 'FixedSingle'
$flowlayoutpanel.Location = '48, 13'
$flowlayoutpanel.Margin = '4, 4, 4, 4'
$flowlayoutpanel.Name = 'flowlayoutpanel1'
$flowlayoutpanel.Size = "400, $flowlayoutsize"
$flowlayoutpanel.TabIndex = 1
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = "383, $buttonplacement"
$buttonOK.Margin = '4, 4, 4, 4'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '100, 30'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$form.ShowDialog()
答案 0 :(得分:0)
用此代码替换你的foreacheach循环
$form_Load = {
foreach($user in $usernames){
$DynamicCheckBox = New-Variable -Name ("checkbox" + $user)
$DynamicCheckBox = New-object System.Windows.Forms.CheckBox
$DynamicCheckBox.Margin = '10, 8, 0, 0'
$DynamicCheckBox.Name = 'checkbox' + $_
$DynamicCheckBox.Size = '200, 22'
$DynamicCheckBox.Text = "" + $user
$DynamicCheckBox.TextAlign = 'MiddleLeft'
$flowlayoutpanel.Controls.Add($DynamicCheckBox)
}
}