Windows 10 Pro 22H2 64bit
Testing/Executing with PowerShell 5.1
---
Hi there,
I'm Having issues with extracting CheckedItems from a checkedlistbox
It seems like '[array]$checkedlistboxExample.CheckedItems' is one step behind with what was checked/unchecked in the GUI
The intent of the PowerShell form is for a user to select what items they want.
Their selections (CheckedItems) are collected, matched to elements in a another array and then processed.
----
Below is a simplified run down of the PowerShell form:
1) Create a PowerShell form in PowerShell Studio, add in a 'CheckedListBox'
2) Create an array with the labels for the checkboxes
- $Example_Options= @'
- EXName,EXValue
- Option1,Value1
- Option2,Value2
- '@
- $Example_Options_Array = $Example_Options | ConvertFrom-Csv
- $Example_Option_Array_ExName = $Example_Options_Array.EXName
- Update-ListBox -ListBox $checkedlistboxExample $Example_Option_Array_ExName
- $checkedlistboxExample_ItemCheck=[System.Windows.Forms.ItemCheckEventHandler]{
- $Example_CheckedItems = [array]$checkedlistboxExample.CheckedItems
- Write-Host $Example_CheckedItems
- }
The 'ItemCheck' event is triggering properly, however the issue I'm having is the contents of $Example_CheckedItems doesn't seem to align 100% with what is selected in the GUI.
It is as though its one selection behind.
In the GUI, tick 'Option1' - Event triggers, 'Write-Host $Example_CheckedItems' comes up empty.
In the GUI, UNtick 'Option1' - Event triggers, 'Write-Host $Example_CheckedItems' writes 'Option1' to console.
In the GUI, tick 'Option1' - nothing written to console
In the GUI, tick 'Option1' and 'Option2' - 'Option1' written to console
In the GUI, tick 'Option1', 'Option2', 'Option3' - ''Open1 Option2' written to console
In the GUI, UNtick 'Option3' with 'Option1' and 'Option2' still ticked - 'Option1 Option2 Option3' written to console
To help visualize it a bit easier, refer to the below screenshot, where the output 'Write-Host $Example_CheckedItems' is separated between lines of hashes.
You can see the 5 instances where the trigger ran in the console.
In the form, I simply checked Option1 to 4 sequentially, then for the 5th action, unticked Option5
Again, it seems like the output is working, its just constantly one step behind.
Not too sure if its because I'm missing a step...like an option to commit changes or something.
What I'm expecting/wanting/hoping for is that, when any of the checkboxes are checked / unchecked in the GUI, that will update a variable containing the currently CheckedItems on screen.
If anyone is able to offer and advice on guidance on how to accomplish that or what I'm doing wrong, I'd be most grateful.
Thanks for your time.