checkedlistbox.CheckedItems one step behind actual selections

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.

This forum is a space to discuss coding involving Graphical User Interfaces (GUI) in PowerShell or using WinForms controls, and technical issues related to development.

- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
brendan013
Posts: 1
Last visit: Thu Nov 07, 2024 8:29 am

checkedlistbox.CheckedItems one step behind actual selections

Post by brendan013 »

SAPIEN PowerShell Studio 2023 5.8.221 64bit
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
  1. $Example_Options= @'
  2. EXName,EXValue
  3. Option1,Value1
  4. Option2,Value2
  5. '@
  6. $Example_Options_Array = $Example_Options | ConvertFrom-Csv
  7. $Example_Option_Array_ExName = $Example_Options_Array.EXName
3) Populate the contents of the CheckedListBox with:
  1. Update-ListBox -ListBox $checkedlistboxExample $Example_Option_Array_ExName
4) Add in a 'ItemCheck' event and configure it as such:
  1. $checkedlistboxExample_ItemCheck=[System.Windows.Forms.ItemCheckEventHandler]{
  2.     $Example_CheckedItems = [array]$checkedlistboxExample.CheckedItems
  3.     Write-Host $Example_CheckedItems
  4. }
Attached is a a copy of the .psf
Example_checkedlistbox.psf
(37.02 KiB) Downloaded 400 times
----
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.
2023-10-27_060643.png
2023-10-27_060643.png (6.38 KiB) Viewed 18603 times
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.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked