【问题标题】:How Do I Access CheckBoxes From a Function如何从函数访问复选框
【发布时间】:2016-09-27 10:22:33
【问题描述】:

我创建了一个表单并动态添加了 CheckBox 和 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()

【问题讨论】:

    标签: winforms powershell checkbox


    【解决方案1】:

    首先,您缺少一个加载System.Drawing 程序集的方括号。

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

    【讨论】:

      【解决方案2】:

      好的,我想通了。我在函数中使用了这些行:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        • 1970-01-01
        • 2012-05-26
        相关资源
        最近更新 更多