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!
Assign values to ListCheckBox items
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.
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.
- PsCustomObject-2
- Posts: 16
- Last visit: Tue Oct 30, 2018 8:04 am
Re: Assign values to ListCheckBox items
This will load strings of any kind and return the value member.
Retrieve the value like this:
$CheckedListBox.SelectedItem.Value
or
$CheckedListBox.CheckedItems | %{$_.Value}
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'
$CheckedListBox.SelectedItem.Value
or
$CheckedListBox.CheckedItems | %{$_.Value}
- PsCustomObject-2
- Posts: 16
- Last visit: Tue Oct 30, 2018 8:04 am
Re: Assign values to ListCheckBox items
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.
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.
Re: Assign values to ListCheckBox items
Summing is trivial in PowerShell:
$CheckedListBox.CheckedItems | Measure-Object Value -Sum
See attached example.
$CheckedListBox.CheckedItems | Measure-Object Value -Sum
See attached example.
- Attachments
-
- Test-CLBLoadCustomeList.psf
- (38.54 KiB) Downloaded 136 times
- PsCustomObject-2
- Posts: 16
- Last visit: Tue Oct 30, 2018 8:04 am
Re: Assign values to ListCheckBox items
Thanks appreciate the example, not my final goal but good reference nonetheless