Page 1 of 1

Returning which button was clicked from a form

Posted: Sun Aug 07, 2016 1:10 am
by jpbobrek
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.     #TODO: Place custom script here
  3.     $MainForm.Hide()
  4. }
  5. $buttonDisagree_Click={
  6.     #TODO: Place custom script here
  7.     $MainForm.Hide()
  8. }
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.

Re: Returning which button was clicked from a form

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

Re: Returning which button was clicked from a form

Posted: Sun Aug 07, 2016 3:42 am
by jvierra
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. If('Yes" -eq Call-EULA_psf){
  3.     #form accepted
  4. }else{
  5.     #form declined
  6. }
Simple example. Notice that it works with no code.

  1. $text = @'
  2.  
  3. An End User License Agreement (EULA) is a legal contract between a
  4. software application author or publisher and the user of that
  5. application. The EULA, often referred to as the "software license,
  6. " is similar to a rental agreement; the user agrees to pay for the
  7. privilege of using the software, and promises the software author
  8. or publisher to comply with all restrictions stated in the EULA.
  9. The user is asked to indicate they that "accept" the terms of the
  10. EULA by opening the shrink wrap on the application package,
  11. breaking the seal on the CD case, sending a card back to the
  12. software publisher, installing the application, executing a
  13. downloadable file, or by simply using the application. The user
  14. can refuse to enter into the agreement by returning the software
  15. product for a refund or clicking "Yes" when prompted to accept
  16. the EULA during an install.
  17.  
  18. '@
  19.  
  20. Add-Type -AssemblyName System.Windows.Forms
  21. $form1=New-Object System.Windows.Forms.Form
  22. $form1.StartPosition='CenterScreen'
  23. $form1.Size = '435,285'
  24.  
  25. $textbox1 = New-Object System.Windows.Forms.TextBox
  26. $textbox1.Multiline = $true
  27. $textbox1.ReadOnly = $true
  28. $textbox1.TabStop = $false
  29. #$textbox1.BorderStyle = 'None'
  30. $textbox1.Location = '5,5'
  31. $textbox1.Size = '400,200'
  32. $textbox1.Text = $text
  33. $form1.Controls.Add($textbox1)
  34.  
  35. $button1 = New-Object System.Windows.Forms.Button
  36. $button1.Location = '10,220'
  37. $button1.Text = 'Accept'
  38. $button1.DialogResult = 'Yes'
  39. $form1.Controls.Add($button1)
  40.  
  41. $button2 = New-Object System.Windows.Forms.Button
  42. $button2.Location = '200,220'
  43. $button2.Text = 'Decline'
  44. $button2.DialogResult = 'No'
  45. $form1.Controls.Add($button2)
  46.  
  47. $form1.ShowDialog()

Re: Returning which button was clicked from a form

Posted: Sun Aug 07, 2016 9:18 am
by jpbobrek
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

Re: Returning which button was clicked from a form

Posted: Sun Aug 07, 2016 9:25 am
by jvierra