Help with GUI

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 8 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
WSUsoftware
Posts: 30
Last visit: Thu Feb 17, 2022 2:52 pm

Help with GUI

Post by WSUsoftware »

I tried searching but all the examples I found fell just short of answering my questions. I am trying to make a Powershell Form (.psf) that contains a "CheckedListBox". Each item in the CheckedListBox corresponds to a program I want to install. I want to be able to select multiple Boxes and then click the "OK" button to make them run. I successfully created a Powershell Script (.ps1) that does this, but to manually type in all the buttons, checkboxes, etc...would make a script about 4,000 lines long. Obviously there is an easier way to do this by making a GUI, but I am not sure how to fill out the "script" portion in the Powershell Form to accomplish this. The first picture below is some of the code from the script . The second and third pictures show the form I have designed in powershell studio as well as the script that I am having trouble understanding. To make the powershell form I am using Powershell Studio 2015.
Attachments
Part one of my manually typed form code
Part one of my manually typed form code
Script Part 1.PNG (42.96 KiB) Viewed 3895 times
Code autogenerated by doubleclicking the objects
Code autogenerated by doubleclicking the objects
Form 2.PNG (63.54 KiB) Viewed 3895 times
The form I created using the GUI tools
The form I created using the GUI tools
Form 1.PNG (26.37 KiB) Viewed 3895 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with GUI

Post by jvierra »

Please post the PSF file. What you have posted is not useful.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with GUI

Post by jvierra »

You should note that a checked listbox is loaded the same way as a listbox.

$checkelistbox1.Items.AddRange(@( 'one','two','three','four'))

This will load a checked listbox with those items.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with GUI

Post by jvierra »

Here is an eample
Attachments
Demo-Checked.psf
(19.79 KiB) Downloaded 169 times
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Help with GUI

Post by dan.potter »

You're going to need a switch to associate the checkboxes with the appropriate program to run as well as a foreach statement to go through each checkbox and run if checked.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with GUI

Post by jvierra »

dan.potter wrote:You're going to need a switch to associate the checkboxes with the appropriate program to run as well as a foreach statement to go through each checkbox and run if checked.
No - not really. Just store the program name in the listbox and retrieve it as you process the checks. Remember that listboxes can be tied to objects or datatables and display any field as well as set the default check state based on a field. We can then retrieve the whole row and any scripts or programs it contains.

No need for switch statements in Forms if you use the forms tools. Think of a listbox as a big, visual switch statement,.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Help with GUI

Post by dan.potter »

I was thinking associate program name to msi which I wouldn't put in the listbox. I think he's using the listbox for output.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Help with GUI

Post by dan.potter »

Using your demo this is what I meant.
  1. $buttonOK_Click = {
  2.    
  3.    
  4.     foreach ($program in $checkedlistbox1.CheckedItems) {
  5.        
  6.         switch ($program) {
  7.            
  8.             chrome{ Write-Host 'chrome selected'; $installfile = 'chrome.msi'}
  9.             'powershell studio'{ Write-Host 'ps studio selected'; $installfile = 'psstudio.exe'}
  10.            
  11.         }
  12.        
  13.        
  14.         Write-Host "installing $installfile"
  15.        
  16.     }
  17. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with GUI

Post by jvierra »

dan.potter wrote:I was thinking associate program name to msi which I wouldn't put in the listbox. I think he's using the listbox for output.
An MSI is an executable. It does not need to find a program. No installer needs a program. Just place the name of the installer file and all options in the checkedlistbox.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Help with GUI

Post by dan.potter »

For display purposes as his first screenshot :)
This topic is 8 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