SOLVED

Button Control - Yes No

%3CLINGO-SUB%20id%3D%22lingo-sub-3021051%22%20slang%3D%22en-US%22%3EButton%20Control%20-%20Yes%20No%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3021051%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHi%2C%20I%20am%20working%20on%20form%20(with%20button%20Submit%20and%20Later)%20using%20PowerShell%20script.%20It%20opens%20a%20pop%20up%20when%20%22Later%22%20button%20on%20the%20form%20is%20clicked.%20The%20pop%20up%20has%20%22Yes%22%20and%20%22No%22%20buttons%20and%20the%20message%20says%20if%20you%20want%20to%20complete%20the%20form%20later.%20If%20yes%20is%20clicked%20the%20form%20closes%20but%20when%20no%20is%20clicked%2C%20I%20want%20the%20pop%20up%20close%20and%20the%20form%20to%20stay%20open.%20My%20script%20is%20as%20below%3A%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24buttonLater_Click%3D%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%23TODO%3A%20Place%20custom%20script%20here%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EAdd-Type%20-AssemblyName%20PresentationCore%2C%20PresentationFramework%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24ButtonType%20%3D%20%5BSystem.Windows.MessageBoxButton%5D%3A%3AYesNo%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24MessageIcon%20%3D%20%5BSystem.Windows.MessageBoxImage%5D%3A%3AWarning%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24MessageBody%20%3D%20%22Do%20you%20want%20to%20complete%20later%3F%22%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24MessageTitle%20%3D%20%22Confirm%22%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%24messageboxf%20%3D%20%5BSystem.Windows.MessageBox%5D%3A%3AShow(%24MessageBody%2C%20%24MessageTitle%2C%20%24ButtonType%2C%20%24MessageIcon)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3Eswitch%20(%24msgBoxInput)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E'Yes'%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24dt%20%3D%20Get-Date%20-Format%20%22yyyyMMddHHmmss%22%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%22User%20%24(%24env%3AUSERNAME.Text)%20deferred%20filling%20out%20form%20%24(%24env%3ACOMPUTERNAME)%20Date%3A%20%24dt%22%20%7C%20Out-File%20-FilePath%20%22%24(%24Global%3AREPO)%24(%24env%3AUSERNAME.Text)-%24(%24env%3ACOMPUTERNAME)-%24dt.deferred%22%20-force%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24formAssetForm.Close()%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E'No'%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%23%23%20Do%20something%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24formAssetForm.visible%20%3D%20%24true%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EWith%20this%20script%2C%20when%20I%20click%20on%20No%2C%20the%20form%20still%20closes.%20Can%20someone%20please%20help%20what%20am%20I%20doing%20wrong%20here%3F%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EThanks%20in%20advance%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3021051%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3030660%22%20slang%3D%22en-US%22%3ERe%3A%20Button%20Control%20-%20Yes%20No%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3030660%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1188273%22%20target%3D%22_blank%22%3E%40hkaur2505%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20I%20wouldn't%20use%20the%20%22system%20yes%20no%22%3C%2FP%3E%3CP%3Ebuild%20your%20own%20yes%20no%20Button%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Drawing%22)%20%0A%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Windows.Forms%22)%0A%24form%20%3D%20New-Object%20System.Windows.Forms.Form%0A%24form.Text%20%3D%20'Windows%20Powershell%20Form%20by%20Flo'%0A%24form.Size%20%3D%20New-Object%20System.Drawing.Size(720%2C360)%20%0A%0A%0A%24Button%20%3D%20New-Object%20System.Windows.Forms.Button%0A%24Button.Location%20%3D%20New-Object%20System.Drawing.Size(10%2C100)%0A%24Button.Size%20%3D%20New-Object%20System.Drawing.Size(280%2C20)%0A%24Button.Text%20%3D%20%22No%22%0A%24form.Controls.Add(%24Button)%0A%0A%24Button1%20%3D%20New-Object%20System.Windows.Forms.Button%0A%24Button1.Location%20%3D%20New-Object%20System.Drawing.Size(300%2C100)%0A%24Button1.Size%20%3D%20New-Object%20System.Drawing.Size(280%2C20)%0A%24Button1.Text%20%3D%20%22Yes%22%0A%24form.Controls.Add(%24Button1)%0A%0A%0A%24Button.Add_Click(%7B%0A%20%20%20%20%5BSystem.Windows.Forms.MessageBox%5D%3A%3AShow(%22Hello%20World.%22%20%2C%20%22My%20Dialog%20Box%22)%0A%7D)%0A%0A%24Button1.Add_Click(%7B%0A%20%20%20%20%5BSystem.Windows.Forms.MessageBox%5D%3A%3AShow(%22No%20world.%22%20%2C%20%22My%20Dialog%20Box%22)%3B%0A%20%20%20%20start%20calc.exe%0A%7D)%0A%0A%24form.Topmost%20%3D%20%24True%0A%24form.Add_Shown(%7B%24form.Activate()%7D)%0A%5Bvoid%5D%20%24form.ShowDialog()%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Senior Member

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

1 Reply
best response confirmed by hkaur2505 (Senior Member)
Solution

@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()