Button Yes No

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.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
mkaur2505
Posts: 6
Last visit: Mon Dec 06, 2021 2:14 pm
Has voted: 1 time

Button Yes No

Post by mkaur2505 »

Hi I am working on a GUI script that opens a pop up when "Later" button on the GUI is clicked. The pop up has "Yes" and "No" buttons and the message says if you want to complete the form later. If yes is clicked the form closes but when no is clicked, I want the pop up close and the form to stay open. My script is as below:
  1. $buttonLater_Click={
  2.     #TODO: Place custom script here
  3.     Add-Type -AssemblyName PresentationCore, PresentationFramework
  4.     $ButtonType = [System.Windows.MessageBoxButton]::YesNo
  5.     $MessageIcon = [System.Windows.MessageBoxImage]::Warning
  6.     $MessageBody = "Do you want to complete later?"
  7.     $MessageTitle = "Confirm"
  8.    
  9.     $messageboxf = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle, $ButtonType, $MessageIcon)
  10.    
  11.     switch ($msgBoxInput)
  12.     {
  13.         'Yes' {
  14.             $dt = Get-Date -Format "yyyyMMddHHmmss"
  15.             "User $($env:USERNAME.Text) deferred filling out form $($env:COMPUTERNAME) Date: $dt" | Out-File -FilePath "$($Global:REPO)$($env:USERNAME.Text)-$($env:COMPUTERNAME)-$dt.deferred" -force
  16.             $formAssetForm.Close()
  17.         }
  18.        
  19.         'No' {
  20.             ## Do something
  21.             $formAssetForm.visible = $true
  22.         }
  23.             }
  24.     }
With this script, when I click on No, the form still closes. Can someone please help what am I doing wrong here?

Thanks in advance
by Alexander Riedel » Wed Dec 01, 2021 12:31 am Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Button Yes No

Post by jvierra »

Be sure the button properties do not define a DialogResult. By default, a button may be defined to terminate the form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Button Yes No

Post by jvierra »

Because you are blindly copying things from teh Internet you are using code that is either unnecessary or code that won' work in WInForms.
  1. $buttonLater_Click={
  2.    
  3.     switch ([System.Windows.MessageBox]::Show('Do you want to complete later?', 'Confirm', 'YesNo', 'Warning')){
  4.         'Yes' {
  5.             $dt = Get-Date -Format 'yyyyMMddHHmmss'
  6.             "User $($env:USERNAME.Text) deferred filling out form $($env:COMPUTERNAME) Date: $dt" |
  7.                 Out-File -FilePath "$($Global:REPO)$($env:USERNAME.Text)-$($env:COMPUTERNAME)-$dt.deferred" -force
  8.             $formAssetForm.Close()
  9.         }
  10.         'No'{
  11.             $formAssetForm.visible = $true
  12.         }
  13.     }
  14. }
mkaur2505
Posts: 6
Last visit: Mon Dec 06, 2021 2:14 pm
Has voted: 1 time

Re: Button Yes No

Post by mkaur2505 »

Thanks for the reply. This still closes the AssetForm once I click on No. I changed the code to below exactly as suggested in your reply:

$buttonLater_Click = {

switch ([System.Windows.MessageBox]::Show('Do you want to complete later?', 'Confirm', 'YesNo', 'Warning'))
{
'Yes' {
$dt = Get-Date -Format 'yyyyMMddHHmmss'
"User $($env:USERNAME.Text) deferred filling out form $($env:COMPUTERNAME) Date: $dt" |
Out-File -FilePath "$($Global:REPO)$($env:USERNAME.Text)-$($env:COMPUTERNAME)-$dt.deferred" -force
$formAssetForm.Close()
}
'No'{
$formAssetForm.visible = $true
}
}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Button Yes No

Post by jvierra »

You have to follow all of the instructions in my reply. The code is not the problem. First learn basic PowerShell then spend some time learning WinForms. Guessing will just waste your time. The button is likely set to return a dialog result.

Remeber we can't see your sreen or yuour code and we cannot help you if you do not know the basics or you can't give accurate explanations of what you are doing.
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: Button Yes No

Post by Alexander Riedel »

Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked