Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a list box in PowerShell
    primarykey
    data
    text
    <p>I'm trying to write a PowerShell script that will include a list box. To make sure I've got my head wrapped around the concepts needed, I've looked up a tutorial on TechNet and tried to duplicate the code provided.</p> <p><a href="http://technet.microsoft.com/en-us/library/ff730949.aspx" rel="nofollow noreferrer">http://technet.microsoft.com/en-us/library/ff730949.aspx</a></p> <p>If I'm following the walkthrough and script properly, it appears that the script is intended to present a dialog box and then output the selected item to the CLI. I've duplicated the dialog box, but I can't get it to write the selection to the CLI. I even modified the last line to explicitly call Write-Output to show the value of $x.</p> <p>Everything about this script appears to work as intended, except for actually writing out any value for $x. Since <code>echo "$x"</code> is perhaps the simplest line in all that code, I can only presume the problem is actually with getting data <em>written</em> to $x instead.</p> <p>Below is cut-and-paste from my PowerShell ISE window, which in turn was cut-and-paste from the aforementioned TechNet article:</p> <pre><code>[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select a Computer" $objForm.Size = New-Object System.Drawing.Size(300,200) $objForm.StartPosition = "CenterScreen" $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objListBox.SelectedItem;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()}) $objForm.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Please select a computer:" $objForm.Controls.Add($objLabel) $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,40) $objListBox.Size = New-Object System.Drawing.Size(260,20) $objListBox.Height = 80 [void] $objListBox.Items.Add("atl-dc-001") [void] $objListBox.Items.Add("atl-dc-002") [void] $objListBox.Items.Add("atl-dc-003") [void] $objListBox.Items.Add("atl-dc-004") [void] $objListBox.Items.Add("atl-dc-005") [void] $objListBox.Items.Add("atl-dc-006") [void] $objListBox.Items.Add("atl-dc-007") $objForm.Controls.Add($objListBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() echo "$x" </code></pre> <p>When the above didn't work as expected, I decided to go step-by-step with the tutorial and re-write the script myself. I modified it a bit to better suit my organizational preferences and to add annotations along the way. However, now even the dialog itself is broken.</p> <pre><code># Load Windows Forms &amp; Drawing classes. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") # Create base form. $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select a Computer" $objForm.Size = New-Object System.Drawing.Size(300,200) $objForm.StartPosition = "CenterScreen" # Configure keyboard intercepts for ESC &amp; ENTER. $objForm.KeyPreview = $true $objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $x = $objListBox.SelectedItem $objForm.Close() } }) $objForm.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $objForm.Close() } }) # Create OK button. $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click({ $x = $objListBox.SelectedItem $objForm.Close() }) $objForm.Controls.Add($OKButton) # Create Cancel button. $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({ $objForm.Close() }) $objForm.Controls.Add($CancelButton) # Add form prompt text. $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Text = "Please select a computer:" $objForm.Controls.Add($objLabel) # Add list box. $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,40) $objListBox.Size = New-Object System.Drawing.Size(260,20) $objListBox.Height = 80 # Populate list. [void] $objListBox.Items.Add("Option1") [void] $objListBox.Items.Add("Option2") [void] $objListBox.Items.Add("Option3") [void] $objListBox.Items.Add("Option4") [void] $objListBox.Items.Add("Option5") [void] $objListBox.Items.Add("Option6") [void] $objListBox.Items.Add("Option7") # Force list box to display on top of other windows. $objForm.TopMost = $true # Display list box. $objForm.Add_Shown({ $objForm.Activate() }) [void] $objForm.ShowDialog() # Show result. $x </code></pre> <p>The second code block results in this:</p> <p><img src="https://i.stack.imgur.com/WnTt8.jpg" alt="Broken list box"></p> <p>What am I doing wrong, here?</p> <p>Note: I'm running PowerShell 3.0 on Windows 7</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload