2014-07-24 24 views
0

在Powershell中,我遇到了System.Windows.Forms.CheckedListBox對象的問題。看起來CheckedItems.Count在檢查項目時不會更新。我已閱讀下面的文章,並嘗試標記爲#3的解決方案,但沒有取得任何成功。如何正確使用Powershell中的CheckedListBox?

Powershell Checkedlistbox handling - checked item count inconsistency and missed click events

我能做些什麼來糾正呢?

下面是我正在查看的代碼,您可以在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(); 
} 

回答

0

好吧,幾個簡單的修改,你應該全部設置。不要將$ CheckedListBox作爲[Ref]傳遞(在參數中以及在123行調用它的地方)。另外,不要將$ FeatureForm傳遞給函數,這是沒有必要的。只需從函數內部直接調用即可。最後,.Count屬性不能設置,所以你的ftnChecked的整個目的是沒有意義的。

所以在這裏我們去,修改1號線:

function AcceptFeatures($CheckedListBox) 

註釋掉18行,有沒有必要爲它。

行更改53:

$FeatureForm.Close() | Out-Null; 

註釋掉線117

修改行123:

$OKButton.add_Click({AcceptFeatures $CheckedListBox}); 

現在的作品。它會拋出一個錯誤,因爲$file從來沒有被定義爲輸出到文件,但它的作用不是這樣。

+0

工作,謝謝! –

相關問題