0

I am trying to modify the code found in the Microsoft Documentation (https://learn.microsoft.com/en-us/powershell/scripting/samples/multiple-selection-list-boxes?view=powershell-7.2) to contain 2 ListBoxes: One single select, and one multiple selected.

This issue is only listBox2 is showing up. What I have so far:

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(600,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)

#Single Select Start

$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 = 'Select Primary USB 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)

#Single Select End

#Multiple Item Select Start

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(310,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Select Secondary USB(s) from the list below:'
$form.Controls.Add($label2)

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

$listBox2.SelectionMode = 'MultiExtended'

#Multiple Item Select End

$testArray = gdr

ForEach($n in $testArray){
    if(($n.Root.Length -lt 4) -And ($n.Root.Length -gt 0) -And ($n.Root -ne "\") -And ($n.Root -ne "C:\")){
        [void] $listBox.Items.Add($n.Root)
        [void] $listBox2.Items.Add($n.Root)
    }
}

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

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox1.SelectedItem
    $y = $listBox2.SelectedItems
    Write-Host "Single Item Selected" $x
    Write-Host "Multiple Items Selected" $y
}

P.S. Can someone please make a new tag for PowerShell-GUI

2
  • 2
    In most of the code you use $listBox, but then you call $form.Controls.Add($listBox1) (notice the trailing 1) instead of $form.Controls.Add($listBox) - use consistent variable names and it'll likely work :) Commented Jan 27, 2022 at 16:53
  • @MathiasR.Jessen Thank you very much, this worked perfectly. Commented Jan 27, 2022 at 17:00

1 Answer 1

1

istbox1 doesn't exist....change:

$form.Controls.Add($listBox1)

to

$form.Controls.Add($listBox)

Or rename the object to $listbox1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.