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.
This topic is 3 years and 9 months 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
ewilhelm009
Posts: 7
Last visit: Thu Mar 28, 2024 11:54 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Thu Mar 28, 2024 11:54 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Thu Mar 28, 2024 11:54 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 405
Last visit: Wed Mar 20, 2024 1:40 pm
Has voted: 1 time
Been upvoted: 1 time

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.         for ($i = $checkedlistboxfiles.Items.Count - 1; $i -ge 0; $i--)
  4.         {
  5.             if ($checkedlistboxfiles.GetItemChecked($i))
  6.             {
  7.                 $file = $checkedlistboxfiles.Items[$i].filename
  8.             }
  9.         }
  10.     }
  11.  
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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: Understanding CheckedListBox controls

Post by Domtar »

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

thank!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
This topic is 3 years and 9 months 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