Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using a variable from within a function

Reply
 
Old 12-23-2008   #1 (permalink)
Jobbsy


 
 

Using a variable from within a function

Having trouble with this - would like to re-use the same function to step
through a number of input boxes before writing to a file - however, the
function drops out afer obtaining the variable ($x) and does not allow me to
re-use it - I'm sure there is a simple solution but have been unable to find
it - trying to add the variable $x to the input file:

$date = ( get-date ).ToString('yyyymmddss')
$file = New-Item -type file "$date-freespace.txt"

#-------------------------------------------------------------------------------
function inputbox {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void]
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = $inputtext
$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,20)
$objLabel.Text = $objectlabeltext
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$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
}
#--------------------------------------------------------------------------
$inputtext = "Enter the external IP address"
$objectlabeltext = "Please enter your info here:"
inputbox
add-content $file $x
get-content $file | out-file 'd:\input.bat'
--
jobbsy@xxxxxx

My System SpecsSystem Spec
Old 12-23-2008   #2 (permalink)
Shay Levy [MVP]


 
 

Re: Using a variable from within a function

Hello Jobbsy,

You can set the textbox 'multiline' property to true and resize it so the
user can input multiple lines and then write it to the file.

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


J> Having trouble with this - would like to re-use the same function to
J> step through a number of input boxes before writing to a file -
J> however, the function drops out afer obtaining the variable ($x) and
J> does not allow me to re-use it - I'm sure there is a simple solution
J> but have been unable to find it - trying to add the variable $x to
J> the input file:
J>
J> $date = ( get-date ).ToString('yyyymmddss')
J> $file = New-Item -type file "$date-freespace.txt"
J> #--------------------------------------------------------------------
J> -----------
J> function inputbox {
J> [void]
J> [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
J> [void]
J> [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.For
J> ms")
J> $objForm = New-Object System.Windows.Forms.Form
J> $objForm.Text = $inputtext
J> $objForm.Size = New-Object System.Drawing.Size(300,200)
J> $objForm.StartPosition = "CenterScreen"
J> $objForm.KeyPreview = $True
J> $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
J> {$x=$objTextBox.Text;$objForm.Close()}})
J> $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
J> {$objForm.Close()}})
J> $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location
J> = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object
J> System.Drawing.Size(75,23) $OKButton.Text = "OK"
J> $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
J> $objForm.Controls.Add($OKButton)
J>
J> $CancelButton = New-Object System.Windows.Forms.Button
J> $CancelButton.Location = New-Object System.Drawing.Size(150,120)
J> $CancelButton.Size = New-Object System.Drawing.Size(75,23)
J> $CancelButton.Text = "Cancel"
J> $CancelButton.Add_Click({$objForm.Close()})
J> $objForm.Controls.Add($CancelButton)
J>
J> $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location
J> = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object
J> System.Drawing.Size(280,20) $objLabel.Text = $objectlabeltext
J> $objForm.Controls.Add($objLabel)
J>
J> $objTextBox = New-Object System.Windows.Forms.TextBox
J> $objTextBox.Location = New-Object System.Drawing.Size(10,40)
J> $objTextBox.Size = New-Object System.Drawing.Size(260,20)
J> $objForm.Controls.Add($objTextBox)
J>
J> $objForm.Topmost = $True
J>
J> $objForm.Add_Shown({$objForm.Activate()})
J> [void] $objForm.ShowDialog()
J> #$x
J> }
J> #--------------------------------------------------------------------
J> ------
J> $inputtext = "Enter the external IP address"
J> $objectlabeltext = "Please enter your info here:"
J> inputbox
J> add-content $file $x
J> get-content $file | out-file 'd:\input.bat'


My System SpecsSystem Spec
Old 12-23-2008   #3 (permalink)
RickB


 
 

Re: Using a variable from within a function

On Dec 23, 7:47*am, Jobbsy <Job...@xxxxxx> wrote:
Quote:

> Having trouble with this - would like to re-use the same function to step
> through a number of input boxes before writing to a file - however, the
> function drops out afer obtaining the variable ($x) and does not allow meto
> re-use it - I'm sure there is a simple solution but have been unable to find
> it - trying to add the variable $x to the input file:
>
> $date = ( get-date ).ToString('yyyymmddss')
> $file = New-Item -type file "$date-freespace.txt"
>
> #--------------------------------------------------------------------------*-----
> function inputbox {
> [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
> [void]
> [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
>
> $objForm = New-Object System.Windows.Forms.Form
> $objForm.Text = $inputtext
> $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,20)
> $objLabel.Text = $objectlabeltext
> $objForm.Controls.Add($objLabel)
>
> $objTextBox = New-Object System.Windows.Forms.TextBox
> $objTextBox.Location = New-Object System.Drawing.Size(10,40)
> $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}
>
> #--------------------------------------------------------------------------
> $inputtext = "Enter the external IP address"
> $objectlabeltext = "Please enter your info here:"
> inputbox
> add-content $file $x
> get-content $file | out-file 'd:\input.bat'
> --
> job...@xxxxxx
The version of $x inside your function is not in the same scope as the
one you are trying to write to the file.
Try replacing $x with $global:x
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can adv function take a variable # of parameters? PowerShell
Get variable data from function to pass back to main for use. VB Script
Replace function on a variable? VB Script
Append string to variable in Function PowerShell
Set a variable in a function then pass it off? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46