Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple InputBox function

Tags:

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" 
like image 631
Rhonda Avatar asked May 29 '15 16:05

Rhonda


People also ask

What is InputBox function?

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.

How do you use InputBox?

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.

What is an InputBox used for in Visual Basic?

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.

What is MsgBox and InputBox function?

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.


1 Answers

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) 
like image 128
Ansgar Wiechers Avatar answered Oct 02 '22 13:10

Ansgar Wiechers