forms - Get value back from a text box class -
we have script written no longer us. new powershell , function not returning value:
# prompt user ip/dns address input. function get-ip { [void] [system.reflection.assembly]::loadwithpartialname("system.drawing") [void] [system.reflection.assembly]::loadwithpartialname("system.windows.forms") # creates message box accepts dns/ip address input. $objform = new-object system.windows.forms.form $objform.text = "user input required" $objform.size = new-object system.drawing.size(300,200) $objform.startposition = "centerscreen" $objform.keypreview = $true $objform.add_keydown({if ($_.keycode -eq "enter") {$x=$objtextbox.text;$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=$objtextbox.text;$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,40) $objlabel.text = "please enter ip address of server want connect to:" $objform.controls.add($objlabel) $objtextbox = new-object system.windows.forms.textbox $objtextbox.location = new-object system.drawing.size(10,70) $objtextbox.size = new-object system.drawing.size(260,20) $objform.controls.add($objtextbox) $objform.topmost = $true $objform.add_shown({$objform.activate()}) [void] $objform.showdialog() } i'm calling such:
$ip = get-ip i enter in value , i'm not getting in $ip.
how capture value of text box?
for martin
i made changes martin suggested , here new code:
# prompt user ip/dns address input. function get-ip { [void] [system.reflection.assembly]::loadwithpartialname("system.drawing") [void] [system.reflection.assembly]::loadwithpartialname("system.windows.forms") # creates message box accepts dns/ip address input. $objform = new-object system.windows.forms.form $objform.text = "user input required" $objform.size = new-object system.drawing.size(300,200) $objform.startposition = "centerscreen" $objform.keypreview = $true $objform.add_keydown({if ($_.keycode -eq "enter") {$script:x=$objtextbox.text;$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({$script:x=$objtextbox.text;$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,40) $objlabel.text = "please enter ip address of server want connect to:" $objform.controls.add($objlabel) $objtextbox = new-object system.windows.forms.textbox $objtextbox.location = new-object system.drawing.size(10,70) $objtextbox.size = new-object system.drawing.size(260,20) $objform.controls.add($objtextbox) $objform.topmost = $true $objform.add_shown({$objform.activate()}) [void] $objform.showdialog() $x } i same error message before:
ps e:\dropbox\powershell scripts\sss cloud icons> .\cloudicons.ps1 creating directories... mkdir : cannot bind argument parameter 'path' because empty string. @ e:\dropbox\powershell scripts\sss cloud icons\cloudicons.ps1:80 char:4 + md $_.username + ~~~~~~~~~~~ + categoryinfo : invaliddata: (:) [mkdir], parameterbindingvalidationexception + fullyqualifiederrorid : parameterargumentvalidationerroremptystringnotallowed,mkdir did miss something?
** update **
i asked how calling get-ip:
# insert user entered ip pipeline. $ip = get-ip # sets user entered ip new file called propanehasip.rdp. (get-content .\propanetest.rdp) -replace 'full address:s:insertiphere',"full address:s:$ip" | out-file propanehasip.rdp write-host "creating directories..." import-csv $csv | foreach-object { # creates directories based on username field. md $_.username # creates icons based on propanehasip.rdp , wsid fields. (get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:insertwsidhere',"remoteapplicationcmdline:s:$($_.wsid1)" | out-file ".\$($_.username)\propane $($_.wsid1).rdp" (get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:insertwsidhere',"remoteapplicationcmdline:s:$($_.wsid2)" | out-file ".\$($_.username)\propane $($_.wsid2).rdp" } } update #2
i figured out how debug in visual studio. error happening on line:
(get-content .\propanetest.rdp) -replace 'full address:s:insertiphere',"full address:s:$ip" | out-file propanehasip_new.rdp
$x valid inside block (the scope of variable $x), if change $script:x , add $x before ending } of function, works. additional $x needed represent return value of function.
changes:
$objform.keypreview = $true $objform.add_keydown({if ($_.keycode -eq "enter") {$script:x=$objtextbox.text;$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({$script:x=$objtextbox.text;$objform.close()}) $objform.controls.add($okbutton) end of function:
$objform.add_shown({$objform.activate()}) [void] $objform.showdialog() $x } and... script taken here , should demonstrate use of windows.forms. however, didnt found date, when posted.
Comments
Post a Comment