Assign values to ListCheckBox items

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 topic is 6 years and 4 months 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
User avatar
PsCustomObject-2
Posts: 16
Last visit: Tue Oct 30, 2018 8:04 am

Assign values to ListCheckBox items

Post by PsCustomObject-2 »

Hello,

as per subject I'm building a form with a listcheckbox control in it, my goal would be to have a value associated with each item something like

Checkedlistbox item 1 = 5
Checkedlistbox item 2 = 7
Checkedlistbox item 3 = 4
Checkedlistbox item 4 = 9
Checkedlistbox item 5 = 2

I use these values as a bitfield mask to control some events and would like to have a way to automatically calculate the value based on items checked by the user, so if user ticks checkbox 1, 2 and 4 I would like to store 21 in a variable or output to another control for what it matters.

Maybe I just need a break from this but I could not find a way to do it. Any pointer would be highly appreciated!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Assign values to ListCheckBox items

Post by jvierra »

This will load strings of any kind and return the value member.

Code: Select all

[array]$items += [pscustomobject]@{Name='one';Value=1}
$items += [pscustomobject]@{Name='two';Value=2}
$items += [pscustomobject]@{Name='three';Value=3}
$items += [pscustomobject]@{Name='four';Value=4}
$checkedlistbox.DisplayMember = 'Name'
Retrieve the value like this:
$CheckedListBox.SelectedItem.Value

or
$CheckedListBox.CheckedItems | %{$_.Value}
User avatar
PsCustomObject-2
Posts: 16
Last visit: Tue Oct 30, 2018 8:04 am

Re: Assign values to ListCheckBox items

Post by PsCustomObject-2 »

Spot-on!

Thanks a lot, I had to work a bit around summing up the various values (that's my final intent) but that was the easy part!

Appreciate your help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Assign values to ListCheckBox items

Post by jvierra »

Summing is trivial in PowerShell:

$CheckedListBox.CheckedItems | Measure-Object Value -Sum

See attached example.
Attachments
Test-CLBLoadCustomeList.psf
(38.54 KiB) Downloaded 136 times
User avatar
PsCustomObject-2
Posts: 16
Last visit: Tue Oct 30, 2018 8:04 am

Re: Assign values to ListCheckBox items

Post by PsCustomObject-2 »

Thanks appreciate the example, not my final goal but good reference nonetheless :-)
This topic is 6 years and 4 months 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