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]