Forum Discussion

hkaur2505's avatar
hkaur2505
Copper Contributor
Dec 01, 2021

Button Control - Yes No

Hi, I am working on form (with button Submit and Later) using PowerShell script. It opens a pop up when "Later" button on the form 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:
$buttonLater_Click={
#TODO: Place custom script here
Add-Type -AssemblyName PresentationCore, PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Do you want to complete later?"
$MessageTitle = "Confirm"

$messageboxf = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle, $ButtonType, $MessageIcon)

switch ($msgBoxInput)
{
'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' {
## Do something
$formAssetForm.visible = $true
}
}
}

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

  • hkaur2505 

    Hi I wouldn't use the "system yes no"

    build your own yes no Button: 

     

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Windows Powershell Form by Flo'
    $form.Size = New-Object System.Drawing.Size(720,360) 
    
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(10,100)
    $Button.Size = New-Object System.Drawing.Size(280,20)
    $Button.Text = "No"
    $form.Controls.Add($Button)
    
    $Button1 = New-Object System.Windows.Forms.Button
    $Button1.Location = New-Object System.Drawing.Size(300,100)
    $Button1.Size = New-Object System.Drawing.Size(280,20)
    $Button1.Text = "Yes"
    $form.Controls.Add($Button1)
    
    
    $Button.Add_Click({
        [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
    })
    
    $Button1.Add_Click({
        [System.Windows.Forms.MessageBox]::Show("No world." , "My Dialog Box");
        start calc.exe
    })
    
    $form.Topmost = $True
    $form.Add_Shown({$form.Activate()})
    [void] $form.ShowDialog()

     

  • Schnittlauch's avatar
    Schnittlauch
    Steel Contributor

    hkaur2505 

    Hi I wouldn't use the "system yes no"

    build your own yes no Button: 

     

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Windows Powershell Form by Flo'
    $form.Size = New-Object System.Drawing.Size(720,360) 
    
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(10,100)
    $Button.Size = New-Object System.Drawing.Size(280,20)
    $Button.Text = "No"
    $form.Controls.Add($Button)
    
    $Button1 = New-Object System.Windows.Forms.Button
    $Button1.Location = New-Object System.Drawing.Size(300,100)
    $Button1.Size = New-Object System.Drawing.Size(280,20)
    $Button1.Text = "Yes"
    $form.Controls.Add($Button1)
    
    
    $Button.Add_Click({
        [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
    })
    
    $Button1.Add_Click({
        [System.Windows.Forms.MessageBox]::Show("No world." , "My Dialog Box");
        start calc.exe
    })
    
    $form.Topmost = $True
    $form.Add_Shown({$form.Activate()})
    [void] $form.ShowDialog()

     

Resources