I'm aware of a simple pop-up function for PowerShell, e.g.:
function popUp($text,$title) { $a = new-object -comobject wscript.shell $b = $a.popup($text,0,$title,0) } popUp "Enter your demographics" "Demographics"
But I am unable to find an equivalent for getting a pop-up to ask for input.
Sure, there is Read-Line, but it prompts from the console.
And then there is this complex function, which seems overkill for a script that will ask for input once or twice:
function getValues($formTitle, $textTitle){ [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = $formTitle $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({$Script:userInput=$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,30) $objLabel.Text = $textTitle $objForm.Controls.Add($objLabel) $objTextBox = New-Object System.Windows.Forms.TextBox $objTextBox.Location = New-Object System.Drawing.Size(10,50) $objTextBox.Size = New-Object System.Drawing.Size(260,20) $objForm.Controls.Add($objTextBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() return $userInput } $schema = getValues "Database Schema" "Enter database schema"
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
Use InputBox to display a simple dialog box so that you can enter information to be used in a macro. The dialog box has an OK button and a Cancel button. If you select the OK button, InputBox returns the value entered in the dialog box. If you select the Cancel button, InputBox returns False.
VBA InputBox is used to prompt the user to enter the values. This message box is used to displaying a message and waits for the user action performed by pressing the button. A text can be return in the text box by using the InputBox function if the user clicks on the OK or Enter button.
InputBox and MsgBox are two useful functions. Each opens a dialog window, which closes when the user responds. The InputBox is used to get input from the user and MsgBox is used for output.
Probably the simplest way is to use the InputBox
method of the Microsoft.VisualBasic.Interaction
class:
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $title = 'Demographics' $msg = 'Enter your demographics:' $text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With