Reputation: 296
In Powershell, I am having an issue with the System.Windows.Forms.CheckedListBox object. It seems that the CheckedItems.Count does not get updated when items are checked. I have read the below post and attempted the solution marked as #3 but did not have any success.
Powershell Checkedlistbox handling - checked item count inconsistency and missed click events
What can I do to correct this?
Below is the code that I am looking at, you can see near the top of the AcceptFeatures function is where I'm seeing the issue. You will see in the SetFeatures function where I have attempted the above mentioned solution.
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();
}
Upvotes: 2
Views: 10208
Reputation: 36297
Ok, a couple simple modifications and you should be all set. Do not pass $CheckedListBox as [Ref] (both in the parameters and where you call it on line 123). Also, don't pass $FeatureForm to the function at all, there's no need. Just call it directly from within the function. Lastly, the .Count property can not be set, so the whole purpose of your ftnChecked is moot.
So here we go, modify line 1 to:
function AcceptFeatures($CheckedListBox)
Comment out line 18, there's no need for it.
Change line 53 to:
$FeatureForm.Close() | Out-Null;
Comment out line 117
Modify line 123 to:
$OKButton.add_Click({AcceptFeatures $CheckedListBox});
It now works. It throws an error because $file
is never defined for it to output to a file, but it works other than that.
Upvotes: 2