Page 1 of 3

Help with GUI

Posted: Wed Jan 27, 2016 4:03 pm
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.

Re: Help with GUI

Posted: Wed Jan 27, 2016 4:13 pm
by jvierra
Please post the PSF file. What you have posted is not useful.

Re: Help with GUI

Posted: Wed Jan 27, 2016 4:16 pm
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.

Re: Help with GUI

Posted: Wed Jan 27, 2016 4:34 pm
by jvierra
Here is an eample

Re: Help with GUI

Posted: Thu Jan 28, 2016 5:17 am
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.

Re: Help with GUI

Posted: Thu Jan 28, 2016 6:09 am
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,.

Re: Help with GUI

Posted: Thu Jan 28, 2016 6:13 am
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.

Re: Help with GUI

Posted: Thu Jan 28, 2016 6:21 am
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. }

Re: Help with GUI

Posted: Thu Jan 28, 2016 6:28 am
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.

Re: Help with GUI

Posted: Thu Jan 28, 2016 6:31 am
by dan.potter
For display purposes as his first screenshot :)