Product, version and build: PowerShell Studio 2017, 5.4.136
(*** Please do not write "latest" as a version, specify a version number ***)
32 or 64 bit version of product: 64 bit
Operating system: Windows 10
32 or 64 bit OS: 64 bit
PowerShell Version: V5
I am using the Checked Listbox, but don't know how to set the checkbox to checked when I add elements to the listbox? Any help would be appreciated!
Checked Listbox
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.
Re: Checked Listbox
[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Checked Listbox
Simply call the $checkedListBox.SetItemChecked() method like so:
Now the very first item in the ListBox will be checked.
- $checkedlistbox1.SetItemChecked(0, $true)
DevinL
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Checked Listbox
It might be easier to do this:
You can also use data binding to set the checked state to a property in the data source object.
- $checkedlistbox1.Items.Add('My Item',$true)
Re: Checked Listbox
Oh, I didn't even know that was an available method.
Thanks, Jvierra.
Thanks, Jvierra.
DevinL
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Checked Listbox
Devin - there are many methods and approaches. The MSDN docs are a very good place to get all of the options. I always review them repeatedly when wantig to do something new with a control. Eventually it all sticks.
The one weakness of the CheckedListBox is that it does not support full data binding as most other control do. There is a replacement on CodePlex that adds databinding for the checkbox.
We can also do the following to bind to the "DataSource".
The one weakness of the CheckedListBox is that it does not support full data binding as most other control do. There is a replacement on CodePlex that adds databinding for the checkbox.
We can also do the following to bind to the "DataSource".
- $form1_Load={
- $path = 'd:\test'
- $files = Get-ChildItem $path -File |
- Select Name,@{n='Oversize';e={if($_.Length -gt 1Kb){'Checked'}else{'Unchecked'}}}
- $dt = ConvertTo-DataTable $files
- $checkedlistbox1.DataSource = $dt
- $checkedlistbox1.DisplayMember = 'Name'
- $checkedlistbox1.ValueMember = 'Oversize'
- #sync data
- (0 .. ($checkedlistbox1.Items.Count-1)) |
- ForEach-Object{
- $checkedlistbox1.SetItemCheckState($_, $checkedlistbox1.Items[$_].Oversize)
- }
- }