I'm trying to capture the data of a CheckBox form by storing the Booleans as an array using a simple "For Each" loop. No matter what variation on the basic structure I've tried, the result is always the same:
The array gets populated correctly during the Click event, as confirmed by some basic checks placed in the loop to display what's happening in the console. This data remains in the array until the form is closed, at which point the array becomes empty.
A second (possibly related) issue I've noted is that if the array is declared outside of the Click event it doesn't get populated correctly and instead show the following pattern:
[Correct sequence]
- Apple
- Pear
- Orange
[Actual result]
- [Empty]
- [Empty]
- ApplePearOrange
This doesn't happen if the array is declared inside the Click event. Regardless of where it's declared though, the array data is wiped completely when the form closes.
Here's the current script I'm using to test things, which includes some checks to compare with the console output, provided further down. I've included the results for both scenarios (declaring the array at the start as a global variable, and inside the click event)
[SCRIPT]
Clear-Host
# Define a global array
$global:arrayGlobal = @()
# Path to the data file
$dataFile = "D:\Path\To\Some\Data.txt"
# Parse text file to get data
$listNames = Get-Content $dataFile | ForEach-Object { ($_ -split '=')[0].Trim() }
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create a form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Example Form"
$form.Size = New-Object System.Drawing.Size(300,500)
$form.StartPosition = "CenterScreen"
$form.Topmost = $true
# Create checkboxes for each item
$verticalPosition = 30 # Initial vertical position for checkboxes
foreach ($item in $listNames) {
$checkBox = New-Object System.Windows.Forms.CheckBox
$checkBox.Text = $item
$checkBox.AutoSize = $true
$checkBox.Location = New-Object System.Drawing.Point(20, $verticalPosition)
$form.Controls.Add($checkBox)
$verticalPosition += 25 # Increment to space checkboxes vertically
}
# Add a button to submit selection
$submitButton = New-Object System.Windows.Forms.Button
$form.AcceptButton = $submitButton
$submitButton.Text = "Submit"
$submitButton.Location = New-Object System.Drawing.Point(100,350)
$submitButton.Add_Click({
$items = $form.Controls | Where-Object { $_.GetType() -eq [System.Windows.Forms.CheckBox] }
Write-Host "Check 1 (total on Click): $($items.Length)"
# Define another array inside the Click event to compare
$arrayCompare = @()
for ($i = 0; $i -lt $items.Length; $i++) {
$item = $items[$i]
$arrayGlobal += $item.Checked
$arrayCompare += $item.Checked
Write-Host = "Check 2 (For Each):" $item.Checked
}
Write-Host "Check 3:" $arrayGlobal.Count "items in Global Array"
Write-Host "Check 3:" $arrayCompare.Count "items in Comparison Array"
# CloseForm
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Close()
})
$form.Controls.Add($submitButton)
# Display the form
$form.ShowDialog()
Write-Host "Check 4:" $arrayGlobal.Count "items in Global Array"
Write-Host "Check 4:" $arrayCompare.Count "items in Comparison Array"
Write-Host "Global Array:" $arrayGlobal
[CONSOLE]
Check 1 (total on Click): 9
= Check 2 (For Each): False
= Check 2 (For Each): True
= Check 2 (For Each): True
= Check 2 (For Each): True
= Check 2 (For Each): False
= Check 2 (For Each): False
= Check 2 (For Each): False
= Check 2 (For Each): False
= Check 2 (For Each): False
Check 3: 1 items in Global Array
Check 3: 9 items in Comparison Array
OK
Check 4: 0 items in Global Array
Check 4: 0 items in Comparison Array
Global Array:
[END]
Any insight or suggestions would be much appreciated!
$script:variable = ...; without that, you'll implicitly create a block-local variable.