Using Checkboxes to Run parts of a script selectively

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
danilv77
Posts: 6
Last visit: Fri Sep 30, 2022 10:44 am
Has voted: 1 time

Using Checkboxes to Run parts of a script selectively

Post by danilv77 »

Hello!

I am trying to run a script (or parts of it) based on if a checkbox is checked or not. I am not sure if I should be using a checkboxlist but I found the documentation on that a little confusing so I just used check boxes. An example would be restoring file a, b, c which are checked and leaving off d, e, f when a button (restore) is clicked. I have been searching this out in these forums but haven't found what I am looking for in this context.
GUI.PNG
GUI.PNG (28.74 KiB) Viewed 432 times
by jvierra » Fri Aug 12, 2022 7:07 pm
You need to learn basic PowerShell with WinForms. The collection controls always return a collection, and you can enumerate the collection. You can add objects to the control and then use object properties to store anything you want.

One method I have used often is to store the string to display in the list or checked list along with a code block to be executed when the list selected items are evaluated.

Example:

$listitems = @(
@{ItemName='item1',Code={<some code to execute>}},
@{ItemName='item1',Code={<some code to execute>}},
... list allitems to display
)
Go to full post
jvierra
Posts: 15298
Last visit: Fri Oct 07, 2022 3:07 am
Answers: 26
Has voted: 4 times
Been upvoted: 29 times

Re: Using Checkboxes to Run parts of a script selectively

Post by jvierra »

This would normally be done with a CheckedListBox.

Outside of this it is not possible to know what you are asking.

I would add an object with the label of the type of file and a property that list the file masks that you would use to retrieve the matching files.
danilv77
Posts: 6
Last visit: Fri Sep 30, 2022 10:44 am
Has voted: 1 time

Re: Using Checkboxes to Run parts of a script selectively

Post by danilv77 »

Thank you for your response. I was afraid it would be something that checkboxlist handled, I just haven't quite figured out how to use that effectively yet. The basic premise of what I am trying to do is that once the user clicks restore only the items that are checked would run a specific part of the script. An example would be say only chrome and signatures were checked, when a user clicks restore it runs the scripts associated with those checkboxes only. I apologize again, I am fairly new to this.
jvierra
Posts: 15298
Last visit: Fri Oct 07, 2022 3:07 am
Answers: 26
Has voted: 4 times
Been upvoted: 29 times

Re: Using Checkboxes to Run parts of a script selectively

Post by jvierra »

You need to learn basic PowerShell with WinForms. The collection controls always return a collection, and you can enumerate the collection. You can add objects to the control and then use object properties to store anything you want.

One method I have used often is to store the string to display in the list or checked list along with a code block to be executed when the list selected items are evaluated.

Example:

$listitems = @(
@{ItemName='item1',Code={<some code to execute>}},
@{ItemName='item1',Code={<some code to execute>}},
... list allitems to display
)
danilv77
Posts: 6
Last visit: Fri Sep 30, 2022 10:44 am
Has voted: 1 time

Re: Using Checkboxes to Run parts of a script selectively

Post by danilv77 »

Thanks! I have it now, that was the nudge I needed to figure out the context to what I needed to do. Thanks again :D
Locked