Creating a list box in PowerShell -
i'm trying write powershell script include list box. make sure i've got head wrapped around concepts needed, i've looked tutorial on technet , tried duplicate code provided.
http://technet.microsoft.com/en-us/library/ff730949.aspx
if i'm following walkthrough , script properly, appears script intended present dialog box , output selected item cli. i've duplicated dialog box, can't write selection cli. modified last line explicitly call write-output show value of $x.
everything script appears work intended, except writing out value $x. since echo "$x"
perhaps simplest line in code, can presume problem getting data written $x instead.
below cut-and-paste powershell ise window, in turn cut-and-paste aforementioned technet article:
[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 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 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"
when above didn't work expected, decided go step-by-step tutorial , re-write script myself. modified bit better suit organizational preferences , add annotations along way. however, dialog broken.
# load windows forms & 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 computer" $objform.size = new-object system.drawing.size(300,200) $objform.startposition = "centerscreen" # configure keyboard intercepts esc & 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 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 display on top of other windows. $objform.topmost = $true # display list box. $objform.add_shown({ $objform.activate() }) [void] $objform.showdialog() # show result. $x
the second code block results in this:
what doing wrong, here?
note: i'm running powershell 3.0 on windows 7
modify ok button click handler this:
$okbutton.add_click({$global:x=$objlistbox.selecteditem;$objform.close()})
note use of global:
scope specifier $x
variable. powershell uses dynamic scoping , employs "copy-on-write" approach variables. is, assuming $x defined in global scope, modifying $x
in function modify local copy of variable. if intent modify global value of $x must use global specifier e.g. $global:x = ...
.
Comments
Post a Comment