Advertisement
PureGremlin

PowerShell user dialog function

Dec 6th, 2022 (edited)
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <## Usage :: newForm "Title bar" "Input prompt" "pre-input example"
  2. ~~~~~~~~
  3. $result = newForm "SSO lookup" "username here" "first.last"
  4. if ($result['Status'] -eq [System.Windows.Forms.DialogResult]::OK)
  5. {
  6.     $x = $result['Data']
  7. }
  8. ~~~~~~~~~~~~~~~~~
  9. ##>
  10. Function newForm([string] $title, [string] $message, [string] $defaultText)
  11. {
  12.  
  13. Add-Type -AssemblyName System.Windows.Forms
  14. Add-Type -AssemblyName System.Drawing
  15.  
  16. $reply = New-Object System.Windows.Forms.Form
  17. $reply.Text = $title
  18. $reply.Size = New-Object System.Drawing.Size(300,200)
  19. $reply.StartPosition = 'CenterScreen'
  20.  
  21. $okButton = New-Object System.Windows.Forms.Button
  22. $okButton.Location = New-Object System.Drawing.Point(75,120)
  23. $okButton.Size = New-Object System.Drawing.Size(75,23)
  24. $okButton.Text = 'OK'
  25. $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  26. $reply.AcceptButton = $okButton
  27. $reply.Controls.Add($okButton)
  28.  
  29. $cancelButton = New-Object System.Windows.Forms.Button
  30. $cancelButton.Location = New-Object System.Drawing.Point(150,120)
  31. $cancelButton.Size = New-Object System.Drawing.Size(75,23)
  32. $cancelButton.Text = 'Cancel'
  33. $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  34. $reply.CancelButton = $cancelButton
  35. $reply.Controls.Add($cancelButton)
  36.  
  37. $label = New-Object System.Windows.Forms.Label
  38. $label.Location = New-Object System.Drawing.Point(10,20)
  39. $label.Size = New-Object System.Drawing.Size(280,20)
  40. $label.Text = $message
  41. $reply.Controls.Add($label)
  42.  
  43. $textBox = New-Object System.Windows.Forms.TextBox
  44. $textBox.Location = New-Object System.Drawing.Point(10,40)
  45. $textBox.Size = New-Object System.Drawing.Size(260,20)
  46. $textBox.Text = $defaultText
  47. $reply.Controls.Add($textBox)
  48.  
  49. $reply.Topmost = $true
  50.  
  51. $reply.Add_Shown({$textBox.Select()})
  52. $result = @{ Status=$reply.ShowDialog();Data=$textbox.text}
  53. $result
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement