在Powershell中,我遇到了System.Windows.Forms.CheckedListBox对象的问题。检查项目时,CheckedItems.Count似乎没有更新。我已阅读以下帖子并尝试将标记为#3的解决方案,但没有取得任何成功。
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 :(得分: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
从未被定义为输出到文件,但它的工作原理不是这样。