Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

working with list boxes

$
0
0

I found this code doing a Google search and then I modified it to search for OUs.   Everything works except I can't write out the OU I choose from the list.   $X seems like it would display my selection but it does not.     So I'm missing something obvious here.  

 

 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$objForm=New-ObjectSystem.Windows.Forms.Form
$objForm.Text="Select an OU"
$objForm.Size=New-ObjectSystem.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-ObjectSystem.Windows.Forms.Button
$OKButton.Location=New-ObjectSystem.Drawing.Size(75,120)
$OKButton.Size=New-ObjectSystem.Drawing.Size(75,23)
$OKButton.Text="OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton=New-ObjectSystem.Windows.Forms.Button
$CancelButton.Location=New-ObjectSystem.Drawing.Size(150,120)
$CancelButton.Size=New-ObjectSystem.Drawing.Size(75,23)
$CancelButton.Text="Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel=New-ObjectSystem.Windows.Forms.Label
$objLabel.Location=New-ObjectSystem.Drawing.Size(10,20)
$objLabel.Size=New-ObjectSystem.Drawing.Size(280,20)
$objLabel.Text="Please select an OU to ping:"
$objForm.Controls.Add($objLabel)

$objListBox=New-ObjectSystem.Windows.Forms.ListBox
$objListBox.Location=New-ObjectSystem.Drawing.Size(10,40)
$objListBox.Size=New-ObjectSystem.Drawing.Size(260,20)
$objListBox.Height= 80

[void] $objListBox.Items.Add("Main Campus")
[void] $objListBox.Items.Add("IT")
[void] $objListBox.Items.Add("HR")
[void] $objListBox.Items.Add("Annex")

$objForm.Controls.Add($objListBox)

$objForm.Topmost=$True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles