如何在Powershell中正确使用CheckedListBox?

如何在Powershell中正确使用CheckedListBox?,powershell,checkedlistbox,Powershell,Checkedlistbox,在Powershell中,我遇到System.Windows.Forms.CheckedListBox对象问题。检查项目时,CheckedItems.Count似乎不会更新。我已经阅读了下面的帖子并尝试了标记为3的解决方案,但没有成功 我能做些什么来纠正这个问题 下面是我正在查看的代码,您可以在AcceptFeatures函数顶部附近看到问题所在。您将在SetFeatures函数中看到我尝试上述解决方案的地方 function AcceptFeatures([REF]$form, [REF]$C

在Powershell中,我遇到System.Windows.Forms.CheckedListBox对象问题。检查项目时,CheckedItems.Count似乎不会更新。我已经阅读了下面的帖子并尝试了标记为3的解决方案,但没有成功

我能做些什么来纠正这个问题

下面是我正在查看的代码,您可以在AcceptFeatures函数顶部附近看到问题所在。您将在SetFeatures函数中看到我尝试上述解决方案的地方

function AcceptFeatures([REF]$form, [REF]$CheckedListBox)
{
    #validate entry

    #Right here, in this if, $CheckedListBox.CheckedItems.Count always evaluates as 0
    #$CheckedListBox.Items.Count evaluates to 21.

    if(($CheckedListBox -eq $null) -or ($CheckedListBox.CheckedItems.Count -eq 0))
    {
        [Windows.Forms.MessageBox]::Show("You must select at least one feature.", "No feature selected", [Windows.Forms.MessageBoxButtons]::OK)
    }

    #Write features
    $Features = "FEATURES="

    foreach ($item in $CheckedListBox.CheckedItems)
    {
        $Script:FeatureHash.Set_Item($item.ToString(), $true);

        switch ($item.ToString())
        {
            "Database engine" {$Features += "SQLENGINE,"}
            "Replication" {$Features += "REPLICATION,"}
            "Full-text and semantic extractions for search" {$Features += "FULLTEXT,"}
            "Data quality services" {$Features += "DQ,"}
            "Analysis services" {$Features += "AS,"}
            "Reporting services - native" {$Features += "RS,"}
            "Reporting services - sharepoint" {$Features += "INCOMPLETE,"}
            "Reporting services add-in for sharepoint products" {$Features += "INCOMPLETE,"}
            "Data quality client" {$Features += "DQC,"}
            "SQL Server data tools" {$Features += "BIDS,"}
            "Client tools connectivity" {$Features += "CONN,"}
            "Integration services" {$Features += "IS,"}
            "Client tools backwards compatibility" {$Features += "BC,"}
            "Client tools SDK" {$Features += "SDK,"}
            "Documentation components" {$Features += "BOL,"}
            "Management tools - basic" {$Features += "SSMS,"}
            "Management tools - advanced" {$Features += "ADV_SSMS,"}
            "Distributed replay controller" {$Features += "DREPLAY_CTLR,"}
            "Distributed replay client" {$Features += "DREPLAY_CLT,"}
            "SQL client connectivity SDK" {$Features += "SNAC_SDK,"}
            "Master data services" {$Features += "MDS,"}
            default {Write-Host "Selected feature (" + $item.ToString() + ") not recognized. Debug script."}
        }
    }

    #Remove trailing comma
    $Features = $Features.Substring(0,$Features.Length - 2);

    #WRITE FEATURES
    $Features |  Out-File $file -Append

    $form.Close() | Out-Null;
}

function InitializeFeatureList([REF]$CheckedListBox)
{
    # Set the list items here to centralize a location for changing the feature list
    if ($CheckedListBox -ne $null)
    {
        $CheckedListBox.Value.Items.Add("Database engine") | Out-Null;
        $CheckedListBox.Value.Items.Add("Replication") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Full-text and semantic extractions for search") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Data quality services") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Analysis services") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Reporting services - native") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Reporting services - sharepoint") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Reporting services add-in for sharepoint products") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Data quality client") | Out-Null;    
        $CheckedListBox.Value.Items.Add("SQL Server data tools") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Client tools connectivity") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Integration services") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Client tools backwards compatibility") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Client tools SDK") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Documentation components") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Management tools - basic") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Management tools - advanced") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Distributed replay controller") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Distributed replay client") | Out-Null;    
        $CheckedListBox.Value.Items.Add("SQL client connectivity SDK") | Out-Null;    
        $CheckedListBox.Value.Items.Add("Master data services") | Out-Null;
    }
}

function ftnChecked()
{
  if ($_.NewValue -eq 'checked')
  {
    $CheckedListBox.CheckedItems.Count ++;
  }
  else
  {
    $CheckedListBox.CheckedItems.Count --;
  }
}

function SetFeatures()
{
    # Create a Form
    $FeatureForm = New-Object -TypeName System.Windows.Forms.Form;
    $FeatureForm.Width = 345;
    $FeatureForm.Height = 389;
    $FeatureForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog;
    $FeatureForm.StartPosition = "CenterScreen";
    $FeatureForm.MaximizeBox = $false;
    $FeatureForm.Text = "Feature selection";
    # Create a CheckedListBox
    $CheckedListBox = New-Object -TypeName System.Windows.Forms.CheckedListBox;
    # Add the CheckedListBox to the Form
    $FeatureForm.Controls.Add($CheckedListBox);
    # Widen the CheckedListBox
    $CheckedListBox.Width = 325;
    $CheckedListBox.Height = 325;
    $CheckedListBox.Left = 5;
    $CheckedListBox.Top = 5;
    $CheckedListBox.CheckOnClick = $true
    $CheckedListBox.Add_ItemCheck({ftnChecked})
    #Create button
    $OKButton = New-Object -TypeName System.Windows.Forms.Button;
    $OKButton.Text = "Accept";
    $OKButton.Top = $CheckedListBox.Top + $CheckedListBox.Height + 2;
    $OKButton.Left = ($FeatureForm.Width / 2) - ($OKButton.Width / 2);
    $OKButton.add_Click({AcceptFeatures ([REF]$FeatureForm) ([REF]$CheckedListBox)});
    #Add button
    $FeatureForm.Controls.Add($OKButton);

    # Add the CheckedListBox to the Form
    InitializeFeatureList ([REF]$CheckedListBox)
    $FeatureForm.Controls.Add($CheckedListBox);

    # Clear all existing selections
    $CheckedListBox.ClearSelected();

    # Show the form
    $FeatureForm.ShowDialog();
}

好的,几个简单的修改,你应该都准备好了。不要将$CheckedListBox作为[Ref]在参数中传递,也不要在第123行调用它的位置传递。另外,根本不要将$FeatureForm传递给函数,没有必要。只需从函数中直接调用它。最后,无法设置.Count属性,因此ftnChecked的全部目的都是没有意义的

现在,我们将第1行修改为:

function AcceptFeatures($CheckedListBox)
注释掉第18行,没有必要

将第53行更改为:

    $FeatureForm.Close() | Out-Null;
注释掉第117行

将第123行修改为:

    $OKButton.add_Click({AcceptFeatures $CheckedListBox});
它现在起作用了。它抛出一个错误,因为$file从未定义用于将其输出到文件,但除此之外它还能工作