Search code examples
powershelluser-interfacelistviewexecution

PowerShell Multiple-selection list boxes


I am trying to utilize multiple-selection list boxes. It is my first attempt. I found this documentation on Microsoft. https://learn.microsoft.com/en-us/powershell/scripting/samples/multiple-selection-list-boxes?source=recommendations&view=powershell-7.3

I want the form to display a list of four different options, each of those options is the name a of script that will be executed if you choose it. How would I add that. I am not sure how to get started on this. Is this functionality possible? Some help in the right direction would be appreciated.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please make a selection from the list below:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)

$listBox.SelectionMode = 'MultiExtended'

[void] $listBox.Items.Add('Item 1')
[void] $listBox.Items.Add('Item 2')
[void] $listBox.Items.Add('Item 3')
[void] $listBox.Items.Add('Item 4')
[void] $listBox.Items.Add('Item 5')

$listBox.Height = 70
$form.Controls.Add($listBox)
$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItems
    $x
}

enter image description here


Solution

  • If I understand correctly, you can use a hashtable before your condition where the hashtable Keys are the items added to the $listBox and the hashtable Values are the paths of the .ps1 scripts you want to run. Then you can use the & call operator to run the selected script:

    $options = @{
        'Item 1' = 'path\to\script1.ps1'
        'Item 2' = 'path\to\script2.ps1'
        'Item 3' = 'path\to\script3.ps1'
        # and so on here
    }
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        # need a loop here in case multiple items are selected
        $options[$listBox.SelectedItems] | ForEach-Object {
            # then for each path, use `&` to invoke the `ps1` script
            & $_
        }
    }