Returning which button was clicked from a form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

Returning which button was clicked from a form

Post by kerbob » Sun Aug 07, 2016 1:10 am

I've got a script that is registering a WMI event. When that even occurs, its calling a form (Call-MainForm_psf). The form only has some agreement verbiage and has two buttons: Agree Disagree

The button code in the form itself only has a command to hide the form (otherwise the form won't close and return to the main script).
  1. $buttonAgree_Click={
  2.  
  3.     #TODO: Place custom script here
  4.  
  5.     $MainForm.Hide()
  6.  
  7. }
  8.  
  9. $buttonDisagree_Click={
  10.  
  11.     #TODO: Place custom script here
  12.  
  13.     $MainForm.Hide()
  14.  
  15. }
So, now I'm back in the main script again.. how can I tell which button was clicked?
I'm sure this is simple, but I can't figure it.
I checked to see if $MainForm_buttonAgree or $MainForm_buttonDisagree had any values, but nada.

jvierra
Posts: 14018
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Returning which button was clicked from a form

Post by jvierra » Sun Aug 07, 2016 3:15 am

The following returns "Yes" or "No"
  1. $text=@'
  2.  
  3. An End User License Agreement (EULA) is a legal contract between a
  4.  
  5. software application author or publisher and the user of that
  6.  
  7. application. The EULA, often referred to as the "software license,
  8.  
  9. " is similar to a rental agreement; the user agrees to pay for the
  10.  
  11. privilege of using the software, and promises the software author
  12.  
  13. or publisher to comply with all restrictions stated in the EULA.
  14.  
  15. The user is asked to indicate they that "accept" the terms of the
  16.  
  17. EULA by opening the shrink wrap on the application package,
  18.  
  19. breaking the seal on the CD case, sending a card back to the
  20.  
  21. software publisher, installing the application, executing a
  22.  
  23. downloadable file, or by simply using the application. The user
  24.  
  25. can refuse to enter into the agreement by returning the software
  26.  
  27. product for a refund or clicking "Yes" when prompted to accept
  28.  
  29. the EULA during an install.
  30.  
  31. '@
  32.  
  33. [System.Windows.Forms.MessageBox]::Show(
  34.  
  35.     $text, 'Please Accept Agreement',
  36.  
  37.     [System.Windows.Forms.MessageBoxButtons]::YesNo,
  38.  
  39.     [System.Windows.Forms.MessageBoxIcon]::Information,
  40.  
  41.     0
  42.  
  43. )

jvierra
Posts: 14018
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Returning which button was clicked from a form

Post by jvierra » Sun Aug 07, 2016 3:42 am

Build your form. Do not add any code. In a dialog the form will automatically close on any button that is assigned a "DialogResult''. Set one button DialogResult to 'Yes' and the other to 'No'.

When complete save to PS1 file then edit the last line:
  1. #Call the form
  2.  
  3. If('Yes" -eq Call-EULA_psf){
  4.  
  5.     #form accepted
  6.  
  7. }else{
  8.  
  9.     #form declined
  10.  
  11. }
Simple example. Notice that it works with no code.

  1. $text = @'
  2.  
  3.  
  4.  
  5. An End User License Agreement (EULA) is a legal contract between a
  6.  
  7. software application author or publisher and the user of that
  8.  
  9. application. The EULA, often referred to as the "software license,
  10.  
  11. " is similar to a rental agreement; the user agrees to pay for the
  12.  
  13. privilege of using the software, and promises the software author
  14.  
  15. or publisher to comply with all restrictions stated in the EULA.
  16.  
  17. The user is asked to indicate they that "accept" the terms of the
  18.  
  19. EULA by opening the shrink wrap on the application package,
  20.  
  21. breaking the seal on the CD case, sending a card back to the
  22.  
  23. software publisher, installing the application, executing a
  24.  
  25. downloadable file, or by simply using the application. The user
  26.  
  27. can refuse to enter into the agreement by returning the software
  28.  
  29. product for a refund or clicking "Yes" when prompted to accept
  30.  
  31. the EULA during an install.
  32.  
  33.  
  34.  
  35. '@
  36.  
  37.  
  38.  
  39. Add-Type -AssemblyName System.Windows.Forms
  40.  
  41. $form1=New-Object System.Windows.Forms.Form
  42.  
  43. $form1.StartPosition='CenterScreen'
  44.  
  45. $form1.Size = '435,285'
  46.  
  47.  
  48.  
  49. $textbox1 = New-Object System.Windows.Forms.TextBox
  50.  
  51. $textbox1.Multiline = $true
  52.  
  53. $textbox1.ReadOnly = $true
  54.  
  55. $textbox1.TabStop = $false
  56.  
  57. #$textbox1.BorderStyle = 'None'
  58.  
  59. $textbox1.Location = '5,5'
  60.  
  61. $textbox1.Size = '400,200'
  62.  
  63. $textbox1.Text = $text
  64.  
  65. $form1.Controls.Add($textbox1)
  66.  
  67.  
  68.  
  69. $button1 = New-Object System.Windows.Forms.Button
  70.  
  71. $button1.Location = '10,220'
  72.  
  73. $button1.Text = 'Accept'
  74.  
  75. $button1.DialogResult = 'Yes'
  76.  
  77. $form1.Controls.Add($button1)
  78.  
  79.  
  80.  
  81. $button2 = New-Object System.Windows.Forms.Button
  82.  
  83. $button2.Location = '200,220'
  84.  
  85. $button2.Text = 'Decline'
  86.  
  87. $button2.DialogResult = 'No'
  88.  
  89. $form1.Controls.Add($button2)
  90.  
  91.  
  92.  
  93. $form1.ShowDialog()

User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

Re: Returning which button was clicked from a form

Post by kerbob » Sun Aug 07, 2016 9:18 am

Thank you, Thank you!

$button1.DialogResult = 'Yes' is the piece I was missing. I set these values on the button properties and called the script per your suggestions and it works great. Simple thing now that I know, but it was kicking my butt! I'm still green. :D

jvierra
Posts: 14018
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Returning which button was clicked from a form

Post by jvierra » Sun Aug 07, 2016 9:25 am


Locked