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.
Locked
User avatar
timstspry17
Posts: 29
Joined: Sun Sep 06, 2015 9:31 pm

Checked Listbox

Post by timstspry17 » Thu Mar 09, 2017 9:51 am

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!

User avatar
DevinL
Site Admin
Posts: 1098
Joined: Mon Aug 17, 2015 2:25 pm

Re: Checked Listbox

Post by DevinL » Thu Mar 09, 2017 10:00 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.

User avatar
DevinL
Site Admin
Posts: 1098
Joined: Mon Aug 17, 2015 2:25 pm

Re: Checked Listbox

Post by DevinL » Thu Mar 09, 2017 10:01 am

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.

User avatar
jvierra
Posts: 12763
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked Listbox

Post by jvierra » Thu Mar 09, 2017 10:32 am

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.

User avatar
DevinL
Site Admin
Posts: 1098
Joined: Mon Aug 17, 2015 2:25 pm

Re: Checked Listbox

Post by DevinL » Thu Mar 09, 2017 11:52 am

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

Thanks, Jvierra.
DevinL
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 12763
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Checked Listbox

Post by jvierra » Thu Mar 09, 2017 12:03 pm

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.    
  4.  
  5.     $path = 'd:\test'
  6.  
  7.     $files = Get-ChildItem $path -File |
  8.  
  9.         Select Name,@{n='Oversize';e={if($_.Length -gt 1Kb){'Checked'}else{'Unchecked'}}}
  10.  
  11.     $dt = ConvertTo-DataTable $files
  12.  
  13.    
  14.  
  15.     $checkedlistbox1.DataSource = $dt
  16.  
  17.     $checkedlistbox1.DisplayMember = 'Name'
  18.  
  19.     $checkedlistbox1.ValueMember = 'Oversize'
  20.  
  21.    
  22.  
  23.     #sync data
  24.  
  25.     (0 .. ($checkedlistbox1.Items.Count-1)) |
  26.  
  27.         ForEach-Object{
  28.  
  29.             $checkedlistbox1.SetItemCheckState($_, $checkedlistbox1.Items[$_].Oversize)
  30.  
  31.         }
  32.  
  33. }

Locked