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).

Code: Select all

$buttonAgree_Click={
#TODO: Place custom script here
$MainForm.Hide()
}
$buttonDisagree_Click={
#TODO: Place custom script here
$MainForm.Hide()
}
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.

User avatar
jvierra
Posts: 13790
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"

Code: Select all

$text=@'
An End User License Agreement (EULA) is a legal contract between a
software application author or publisher and the user of that
application. The EULA, often referred to as the "software license,
" is similar to a rental agreement; the user agrees to pay for the
privilege of using the software, and promises the software author
or publisher to comply with all restrictions stated in the EULA.
The user is asked to indicate they that "accept" the terms of the
EULA by opening the shrink wrap on the application package,
breaking the seal on the CD case, sending a card back to the
software publisher, installing the application, executing a
downloadable file, or by simply using the application. The user
can refuse to enter into the agreement by returning the software
product for a refund or clicking "Yes" when prompted to accept
the EULA during an install.
'@
[System.Windows.Forms.MessageBox]::Show(
$text, 'Please Accept Agreement',
[System.Windows.Forms.MessageBoxButtons]::YesNo,
[System.Windows.Forms.MessageBoxIcon]::Information,
0
)

User avatar
jvierra
Posts: 13790
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:

Code: Select all

#Call the form
If('Yes" -eq Call-EULA_psf){
#form accepted
}else{
#form declined
}
Simple example. Notice that it works with no code.

Code: Select all

$text = @'

An End User License Agreement (EULA) is a legal contract between a
software application author or publisher and the user of that
application. The EULA, often referred to as the "software license,
" is similar to a rental agreement; the user agrees to pay for the
privilege of using the software, and promises the software author
or publisher to comply with all restrictions stated in the EULA.
The user is asked to indicate they that "accept" the terms of the
EULA by opening the shrink wrap on the application package,
breaking the seal on the CD case, sending a card back to the
software publisher, installing the application, executing a
downloadable file, or by simply using the application. The user
can refuse to enter into the agreement by returning the software
product for a refund or clicking "Yes" when prompted to accept
the EULA during an install.

'@

Add-Type -AssemblyName System.Windows.Forms
$form1=New-Object System.Windows.Forms.Form
$form1.StartPosition='CenterScreen'
$form1.Size = '435,285'

$textbox1 = New-Object System.Windows.Forms.TextBox
$textbox1.Multiline = $true
$textbox1.ReadOnly = $true
$textbox1.TabStop = $false
#$textbox1.BorderStyle = 'None'
$textbox1.Location = '5,5'
$textbox1.Size = '400,200'
$textbox1.Text = $text
$form1.Controls.Add($textbox1)

$button1 = New-Object System.Windows.Forms.Button
$button1.Location = '10,220'
$button1.Text = 'Accept'
$button1.DialogResult = 'Yes'
$form1.Controls.Add($button1)

$button2 = New-Object System.Windows.Forms.Button
$button2.Location = '200,220'
$button2.Text = 'Decline'
$button2.DialogResult = 'No'
$form1.Controls.Add($button2)

$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

User avatar
jvierra
Posts: 13790
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