如何从函数访问CheckBoxes

时间:2016-09-26 04:48:42

标签: winforms powershell checkbox

我已经创建了一个表单并动态添加了CheckBoxes和CheckBox名称。如何以编程方式从Get-LicenseDetails函数中检查一个特定的CheckBox?缺少支架已添加。

import-module MSOnline

Function Get-LicenseDetails {
    Param ($upn)
    $licenses = Get-MsolUser -UserPrincipalName $upn
    ForEach ($license in $licenses.Licenses) {
        If ($license.AccountSkuId -like '*ENTERPRISEPACK') {
            $serviceName = $serviceStatus.ServicePlan.ServiceName
            $checkBox.Checked = $true
        }
    }
}

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$System_Drawing_Point = New-Object System.Drawing.Point
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object Windows.Forms.Form
$form.Text = "Office 365 Licensing"
$form.Name = "Form1"
$form.Size = New-Object Drawing.Size @(316, 510)

#SEARCH BUTTON
$searchBtn = New-Object System.Windows.Forms.Button
$System_Drawing_Point.X = 226 
$System_Drawing_Point.Y = 38 
$searchBtn.Location = $System_Drawing_Point 
$searchBtn.add_click({Get-LicenseDetails "user.name@domain.com"})
$searchBtn.Size = New-Object System.Drawing.Size(67, 23)
$searchBtn.Text = "Click Me"
$form.Controls.Add($searchBtn)

#CHECKBOXES
$y = 80
$Services = (Get-MsolAccountSku | Where-Object {$_.SkuPartNumber -eq "ENTERPRISEPACK"}).ServiceStatus.ServicePlan.ServiceName
ForEach ($service in $Services) {
    $checkbox = New-Object System.Windows.Forms.CheckBox
    $checkbox.Text = $service
    $checkbox.Name = "CheckBox_$service"
    $checkbox.Size = New-Object System.Drawing.Size(260,17)
    $checkbox.Location = New-Object System.Drawing.Size(10,$y) 
    $y += 25
    $form.Controls.Add($checkbox) 
}

$drc = $form.ShowDialog()

2 个答案:

答案 0 :(得分:0)

首先,您缺少加载System.Drawing装配体的方括号。

您可以访问Get-LicenseDetails bei中的CheckBox,将其传递给函数或使用$global:访问它们。但是,我不会将GUI元素传递给该函数。相反,我会创建一个模型(新对象)并将其传递给Get-LicenseDetails方法。

答案 1 :(得分:0)

好的,我已经弄清楚了。我在函数中使用了这些行:

$checkBoxName = "CheckBox_" + $serviceName
$checkbox = $form.Controls | Where-Object {$_.name -eq $checkBoxName}
$checkbox.Checked = $true