Last active
June 5, 2016 17:05
-
-
Save midnightfreddie/98ee7ee76f1f69ef37f41e66bc45e191 to your computer and use it in GitHub Desktop.
In reply to https://www.reddit.com/r/PowerShell/comments/4mnqib/powershell_gui_form_input_does_not_return_anything/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | |
# I hate Write-Host, but the console output stream is uncoupled from the form, so we'll do this for illustration | |
function Write-MyOutput ($x) { | |
Write-Host "Entered Text: $x" | |
} | |
$objForm = New-Object System.Windows.Forms.Form | |
$objForm.Text = "Data Entry Form" | |
$objForm.Size = New-Object System.Drawing.Size(300,200) | |
$objForm.StartPosition = "CenterScreen" | |
$objForm.KeyPreview = $True | |
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") { | |
Write-MyOutput $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({ | |
Write-MyOutput $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 = "Please enter the information in the space below:" | |
$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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment