0

The fields in the form aren't being set by the button/click events. Can anyone see what I'm doing wrong? The vars I declare at the top are supposed to be set to the textbox values when there is a click/button press. For some reason, they aren't being set by the function, and the function only returns whatever I declare them to at the top.

function global:fPxShow-PasswordForm ([string] $user, [string] $environment){
[string] $x = "" #this variable is not set by the click/button events for some reason
[string] $y = "" #this variable is not set by the click/button events for some reason
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Password Entry Form"
$objForm.Size = New-Object System.Drawing.Size(270,250) 
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objTextBox.Text;$y=$objTextBox2.Text;$objForm.Close()}}) #vars should be getting set here
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,90) 
$objTextBox.Size = New-Object System.Drawing.Size(240,20)
$objTextBox.Font = New-Object System.Drawing.Font "Arial",10
$objTextBox.UseSystemPasswordChar = $true
$objForm.Controls.Add($objTextBox) 

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Location = New-Object System.Drawing.Size(10,145) 
$objTextBox2.Size = New-Object System.Drawing.Size(240,20)
$objTextBox2.Font = New-Object System.Drawing.Font "Arial",10
$objTextBox2.UseSystemPasswordChar = $true
$objForm.Controls.Add($objTextBox2) 

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(50,185)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$y=$objTextBox2.Text;$objForm.Close()}) #vars should be getting set here
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(140,185)
$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,10) 
$objLabel.Size = New-Object System.Drawing.Size(240,20) 
$objLabel.Font = New-Object System.Drawing.Font "Courier", 10
$objLabel.Text = "Please enter the password for:"
$objForm.Controls.Add($objLabel) 

$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,40)
$objLabel2.Size = New-Object System.Drawing.Size(100,20) 
$objLabel2.Font = New-Object System.Drawing.Font "Courier", 10
$objLabel2.Text = "Environment:"
$objForm.Controls.Add($objLabel2) 

$objLabel3 = New-Object System.Windows.Forms.Label
$objLabel3.Location = New-Object System.Drawing.Size(10,60)
$objLabel3.Size = New-Object System.Drawing.Size(100,20) 
$objLabel3.Font = New-Object System.Drawing.Font "Courier", 10
$objLabel3.Text = "User Name:"
$objForm.Controls.Add($objLabel3) 

$objLabel4 = New-Object System.Windows.Forms.Label
$objLabel4.Location = New-Object System.Drawing.Size(110,40)
$objLabel4.Size = New-Object System.Drawing.Size(140,20) 
$objLabel4.Font = New-Object System.Drawing.Font "Courier", 10
$objLabel4.Text = "$environment"
$objForm.Controls.Add($objLabel4) 

$objLabel5 = New-Object System.Windows.Forms.Label
$objLabel5.Location = New-Object System.Drawing.Size(110,60)
$objLabel5.Size = New-Object System.Drawing.Size(140,20) 
$objLabel5.Font = New-Object System.Drawing.Font "Courier", 10
$objLabel5.Text = "$user"
$objForm.Controls.Add($objLabel5) 

$objLabel6 = New-Object System.Windows.Forms.Label
$objLabel6.Location = New-Object System.Drawing.Size(10,125)
$objLabel6.Size = New-Object System.Drawing.Size(240,20) 
$objLabel6.Font = New-Object System.Drawing.Font "Courier", 8
$objLabel6.Text = "Enter the password again"
$objForm.Controls.Add($objLabel6) 

$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$objForm.dispose()
Return $x,$y #these vars will not return anything other than the values I declare them to at the top
#Return "testX","testY"

}

$user = "testuser"
$environment = "testenvironment"
$result = "test1","test2"

$result = fPxShow-PasswordForm $user $environment
Write-Output "0:"
Write-Output $result[0]
Write-Output "1:"
Write-Output $result[1]
  • 2
    Try `$script:x = ...`. I think the message handlers run in a new scope so they only set their own copy of the variables, unless you explicitly use a different scope. See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2 – zett42 Mar 11 '22 at 22:46
  • 1
    In short: Event handlers run in a _child scope_, so in order to update a variable in the script's scope, you must use `$script:variable = ...`, otherwise you'll implicitly create a _block-local_ variable. See [this answer](https://stackoverflow.com/a/57649395/45375) to the linked duplicate. – mklement0 Mar 11 '22 at 23:19
  • 1
    In this case, a hashtable seems a more appropriate to use as reference. Instead of `$x` and `$y` define `$ref = @{ x = ''; y = '' }` then in your event's code you can do `$ref['x'] = $objTextBox.Text` and so on. – Santiago Squarzon Mar 11 '22 at 23:22

0 Answers0