Support Forums

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.
User avatar
hamsandwich
Posts: 14
Joined: Thu Nov 20, 2014 12:05 pm

Help with GUI

Postby hamsandwich » Wed Jan 27, 2016 5:03 pm

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
Script Part 1.PNG
Part one of my manually typed form code
Script Part 1.PNG (42.96 KiB) Viewed 1145 times
Form 2.PNG
Code autogenerated by doubleclicking the objects
Form 2.PNG (63.54 KiB) Viewed 1145 times
Form 1.PNG
The form I created using the GUI tools
Form 1.PNG (26.37 KiB) Viewed 1145 times
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with GUI

Postby jvierra » Wed Jan 27, 2016 5:13 pm

Please post the PSF file. What you have posted is not useful.
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with GUI

Postby jvierra » Wed Jan 27, 2016 5:16 pm

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.
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with GUI

Postby jvierra » Wed Jan 27, 2016 5:34 pm

Here is an eample
Attachments
Demo-Checked.psf
(19.79 KiB) Downloaded 58 times
User avatar
dan.potter
Posts: 632
Joined: Wed Oct 08, 2014 6:02 am

Re: Help with GUI

Postby dan.potter » Thu Jan 28, 2016 6:17 am

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.
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with GUI

Postby jvierra » Thu Jan 28, 2016 7:09 am

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: 632
Joined: Wed Oct 08, 2014 6:02 am

Re: Help with GUI

Postby dan.potter » Thu Jan 28, 2016 7:13 am

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: 632
Joined: Wed Oct 08, 2014 6:02 am

Re: Help with GUI

Postby dan.potter » Thu Jan 28, 2016 7:21 am

Using your demo this is what I meant.

$buttonOK_Click = {
 
 
foreach ($program in $checkedlistbox1.CheckedItems) {
 
switch ($program) {
 
chrome{ Write-Host 'chrome selected'; $installfile = 'chrome.msi'}
'powershell studio'{ Write-Host 'ps studio selected'; $installfile = 'psstudio.exe'}
 
}
 
 
Write-Host "installing $installfile"
 
}
}
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with GUI

Postby jvierra » Thu Jan 28, 2016 7:28 am

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: 632
Joined: Wed Oct 08, 2014 6:02 am

Re: Help with GUI

Postby dan.potter » Thu Jan 28, 2016 7:31 am

For display purposes as his first screenshot :)

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest