Understanding CheckedListBox controls

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.
ewilhelm009
Posts: 7
Joined: Fri May 15, 2020 7:21 am

Understanding CheckedListBox controls

Post by ewilhelm009 »

I'm having trouble understanding how CheckedListBox controls work. So I create the collection of items within it, how does powershell studio use these items? Is each item its own variable? I am reading the Winforms control reference, the info center, etc but cannot seem to grasp how this works. I am creating a form so that a user can copy files to a directory on a number of servers. The user would check the check box and then when the remaining scripts are run, it would copy to the servers that were checked. Is there a way to view the checked items and see them in the console? I am assuming that the checked items are $Checkedboxlist.CheckedItems.

Is there a method for checking certain boxes when a particular check box is checked? (example - "All servers" check box would select all servers, or a check box for a group of servers in a certain geographic area).

jvierra
Posts: 14481
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Understanding CheckedListBox controls

Post by jvierra »

Item in a ListBox of any time are stored as an array in the "Items" property of the ListBox.

See the following article for instructions on how to use this control:

https://info.sapien.com/index.php/guis/ ... ew-control

ewilhelm009
Posts: 7
Joined: Fri May 15, 2020 7:21 am

Re: Understanding CheckedListBox controls

Post by ewilhelm009 »

So the items in the array, are verbatim? So if I have "Server1" it will be $Server1?

jvierra
Posts: 14481
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Understanding CheckedListBox controls

Post by jvierra »

No - the item in the array will be the value of $server1 and not the variable itself. If $server1 is an object then the object will have a reference in the list and be accessible through the list. Objects variables are always references to objects and not the object. Objects are structures stored in memory.

ewilhelm009
Posts: 7
Joined: Fri May 15, 2020 7:21 am

Re: Understanding CheckedListBox controls

Post by ewilhelm009 »

Ok so I need to be able to build a variable list and reference the items in the list to the variables?

jvierra
Posts: 14481
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Understanding CheckedListBox controls

Post by jvierra »

YOur question is far too vague.
I recomend starting with this as it will help you understand what variables and lists are in programming as your view of this is based on everyday usage.

Windows PowerShell™ 4: TFM

User avatar
Lembasts
Posts: 301
Joined: Thu Mar 15, 2012 9:47 am

Re: Understanding CheckedListBox controls

Post by Lembasts »

an example would be loading the checkedlistbox like so:

$checkedlistboxfiles.DisplayMember = "filename"
$checkedlistboxfiles.items.addrange(@($fileobject))
This assumes you have a $fileobject with a property called filename.
You might then have a button that wants to do something to all the files that you have 'checked'.
The button_click event would have code like this:
  1.     if ($checkedlistboxfiles.checkedIndices.count -gt 0)
  2.  
  3.     {
  4.  
  5.         for ($i = $checkedlistboxfiles.Items.Count - 1; $i -ge 0; $i--)
  6.  
  7.         {
  8.  
  9.             if ($checkedlistboxfiles.GetItemChecked($i))
  10.  
  11.             {
  12.  
  13.                 $file = $checkedlistboxfiles.Items[$i].filename
  14.  
  15.             }
  16.  
  17.         }
  18.  
  19.     }
  20.  
  21.  
In the middle of the loops I retrieve the filename into $file which contains a filename that has been 'checked'.
Is that the sort of thing you were after?

jvierra
Posts: 14481
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Understanding CheckedListBox controls

Post by jvierra »

To get a CheckedListBox item when it is checked just put your code into the event for the "ItemCheck" event. It will had you the item being checked.

https://docs.microsoft.com/en-us/dotnet ... mework-4.8

The item is "$_.CurrentItem",

User avatar
Domtar
Posts: 90
Joined: Sun Mar 03, 2013 12:42 pm

Re: Understanding CheckedListBox controls

Post by Domtar »

how can we skip the need to click an item twice to check it?

thank!

jvierra
Posts: 14481
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Understanding CheckedListBox controls

Post by jvierra »

Set the property to check on click. "CheckOnClick"

https://docs.microsoft.com/en-us/dotnet ... etcore-3.1

Locked