I've been trying to find this by myself but I'm not entirely sure even how to phrase the question. Essentially what I'm doing here is taking an array of Names, and from that creating a checkbox for each name. I've got it to create a new Checkbox for each object with a variable name using the line:
New-object System.Windows.Forms.CheckBox | New-Variable -Name ("checkbox" + $_)
The main problem I'm having is this: How do I actually set the properties on these variable named checkboxes once they've been created? Essentially how to get the variable name for each iteration, then do the $checkbox.margin and such settings. I've posted my entire script below, it's very WIP and I know there's some other stuff that's weird but I'm planning to sort that out once I can fix this.
$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()
Aucun commentaire:
Enregistrer un commentaire