Checked Listbox

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 7 years 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
User avatar
timstspry17
Posts: 45
Last visit: Tue Apr 18, 2023 2:49 pm

Checked Listbox

Post by timstspry17 »

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!
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Checked Listbox

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Checked Listbox

Post by DevinL »

Simply call the $checkedListBox.SetItemChecked() method like so:
  1. $checkedlistbox1.SetItemChecked(0, $true)
Now the very first item in the ListBox will be checked.
DevinL
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checked Listbox

Post by jvierra »

It might be easier to do this:
  1. $checkedlistbox1.Items.Add('My Item',$true)
You can also use data binding to set the checked state to a property in the data source object.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Checked Listbox

Post by DevinL »

Oh, I didn't even know that was an available method.

Thanks, Jvierra.
DevinL
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checked Listbox

Post by jvierra »

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".
  1. $form1_Load={
  2.    
  3.     $path = 'd:\test'
  4.     $files = Get-ChildItem $path -File |
  5.         Select Name,@{n='Oversize';e={if($_.Length -gt 1Kb){'Checked'}else{'Unchecked'}}}
  6.     $dt = ConvertTo-DataTable $files
  7.    
  8.     $checkedlistbox1.DataSource = $dt
  9.     $checkedlistbox1.DisplayMember = 'Name'
  10.     $checkedlistbox1.ValueMember = 'Oversize'
  11.    
  12.     #sync data
  13.     (0 .. ($checkedlistbox1.Items.Count-1)) |
  14.         ForEach-Object{
  15.             $checkedlistbox1.SetItemCheckState($_, $checkedlistbox1.Items[$_].Oversize)
  16.         }
  17. }
This topic is 7 years 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