Advertisement
atheos42

Get-Input

Jul 23rd, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2
  2. #https://stackoverflow.com/questions/30534273/simple-inputbox-function
  3. ##https://stackoverflow.com/questions/54037292/folderbrowserdialog-bring-to-front
  4. function Get-Input
  5. {
  6.     Param ( [Parameter(Mandatory=0)][string]$Message='Message:',
  7.             [Parameter(Mandatory=0)][string]$Title = 'Title'
  8.     )
  9.  
  10.     [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  11.     [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  12.    
  13.     $form = New-Object System.Windows.Forms.Form
  14.     $form.Text = $Title
  15.     $form.Size = New-Object System.Drawing.Size(300,200)
  16.     $form.StartPosition = 'CenterScreen'
  17.  
  18.     $okButton = New-Object System.Windows.Forms.Button
  19.     $okButton.Location = New-Object System.Drawing.Point(75,120)
  20.     $okButton.Size = New-Object System.Drawing.Size(75,23)
  21.     $okButton.Text = 'OK'
  22.     $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  23.     $form.AcceptButton = $okButton
  24.     $form.Controls.Add($okButton)
  25.  
  26.     $cancelButton = New-Object System.Windows.Forms.Button
  27.     $cancelButton.Location = New-Object System.Drawing.Point(150,120)
  28.     $cancelButton.Size = New-Object System.Drawing.Size(75,23)
  29.     $cancelButton.Text = 'Cancel'
  30.     $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  31.     $form.CancelButton = $cancelButton
  32.     $form.Controls.Add($cancelButton)
  33.  
  34.     $label = New-Object System.Windows.Forms.Label
  35.     $label.Location = New-Object System.Drawing.Point(10,20)
  36.     $label.Size = New-Object System.Drawing.Size(280,20)
  37.     $label.Text = $Message
  38.     $form.Controls.Add($label)
  39.  
  40.     $textBox = New-Object System.Windows.Forms.TextBox
  41.     $textBox.Location = New-Object System.Drawing.Point(10,40)
  42.     $textBox.Size = New-Object System.Drawing.Size(260,20)
  43.     $form.Controls.Add($textBox)
  44.  
  45.     $form.Topmost = $true
  46.  
  47.     $form.Add_Shown({$textBox.Select()})
  48.  
  49.     $hndl = [System.Windows.Forms.NativeWindow]::new()
  50.     $hndl.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
  51.     $result = $form.ShowDialog($hndl)
  52.  
  53.     if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  54.     {
  55.         return $textBox.Text
  56.     }else{
  57.         return $null
  58.     }
  59. }
  60.  
  61. Clear-Host
  62. Get-Input 'Message:' 'Title'
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement